【问题标题】:Exception "The URI prefix is not recognized" from WebClient.DownloadDataAsync来自 WebClient.DownloadDataAsync 的异常“无法识别 URI 前缀”
【发布时间】:2021-09-28 21:23:53
【问题描述】:

我试图从 URL 保存图像,但内部异常中有一个内部异常,告诉我无法识别 URi 前缀。

完全例外:

[ERROR] FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: 
An exception occurred during the operation, making the result invalid. 
Check InnerException for exception details. ---> 
    System.Net.WebException: An exception occurred during a WebClient request. --->
         System.NotSupportedException: The URI prefix is not recognized.

一些代码:

imagen_tap.Tapped += async (s, e) =>
{
   Console.WriteLine("URL IMAGE BRO::::::::: " + imagen.Source.ToString());
   await api.DownloadImage(new Uri(imagen.Source.ToString()));
   string path = Preferences.Get("filePath", "");
   await Launcher.OpenAsync(new OpenFileRequest { File = new ReadOnlyFile(path) });
};


public async Task DownloadImage(Uri URL)
        {
            WebClient webClient = new WebClient();

            string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Images", "temp");
            string fileName = URL.ToString().Split('/').Last();
            string filePath = Path.Combine(folderPath, fileName);

            webClient.DownloadDataCompleted += (s, e) =>
            {
                Directory.CreateDirectory(folderPath);

                File.WriteAllBytes(filePath, e.Result); //Broken?
            };

            webClient.DownloadDataAsync(URL);

            Preferences.Set("filePath", filePath);
        }

编辑:(忘记添加这个) (不是真实网址) 网址是https://api.com/imagenes/partes/18005/2021062311191329243400.jpg

编辑:(回答 Jason,确定 webClient 上的 URL 是否正确)

Screenshot URI on webClient

Screenshot Opening localstorage

Screenshot Broken Line

Screenshot e in debug

编辑:

嗯...我修复了 Uri 前缀...

我改变了两件事: 在下载图像中,我将其从任务更改为字符串并返回一个字符串。 代码:

public string DownloadImageString(string URL)
        {
            WebClient webClient = new WebClient();

            string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Images", "temp");
            string fileName = URL.ToString().Split('/').Last();
            string filePath = Path.Combine(folderPath, fileName);

            webClient.DownloadDataCompleted += (s, e) =>
            {
                Directory.CreateDirectory(folderPath);

                File.WriteAllBytes(filePath, e.Result);
            };

            webClient.DownloadDataAsync(new Uri(URL));

            return filePath;
        }

然后我改变了发送 URL 的方式。

而不是发送一个新的 Uri(url) 我正在发送一个字符串。 该字符串再次手动安装,而不是通过获取 imagen.Source (这是真正的修复) 代码:

Image imagen = new Image
                {
                    Source = url + Foto.ID_Parte + "/" + Foto.Archivo
                };
                var imagen_tap = new TapGestureRecognizer();
                imagen_tap.Tapped += async (s, e) =>
                {
                    string path = api.DownloadImageString(url + Foto.ID_Parte + "/" + Foto.Archivo);
                    await Launcher.OpenAsync(new OpenFileRequest { File = new ReadOnlyFile(path) });
                };
                
                imagen.GestureRecognizers.Add(imagen_tap);

【问题讨论】:

  • 如果您有任何问题,请在帖子中提出。
  • 并告诉哪个uri前缀。
  • "无法识别 URI 前缀。"似乎是一个非常明确的信息。您尝试下载的 URI 是什么?
  • 已编辑问题,我实际上忘记发布网址
  • 哪一行抛出异常?

标签: android ios image xamarin.forms system.io.file


【解决方案1】:

兄弟,您无需手动管理图像的延迟加载和本地存储,只需使用库 FFImageLoading 即可,它将节省您的时间并提高您的应用性能

此库在 Xamarin.iOS、Xamarin.Android、Xamarin.Forms 上快速轻松地加载图像

请确认此答案是否适合您!

【讨论】:

  • 问题是,这个帖子是尝试解决另一个帖子 xD 我想在 Android/IOS 上的默认图库 APP 上打开一个图像 有人告诉我使用一个功能。但该功能只适用于本地图片,所以我想:下载图片并打开它...帖子是:stackoverflow.com/questions/68437194
【解决方案2】:

所以,修复有效,我现在正在手机中进行测试,它工作正常,修复在最后一次编辑中。

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多