来自 WebClient.DownloadDataAsync 的异常 "The URI prefix is not recognized"

Exception "The URI prefix is not recognized" from WebClient.DownloadDataAsync

我试图从 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);
        }

编辑:(忘记添加) (不是真实的 URL) URL 是 https://api.com/imagenes/partes/18005/2021062311191329243400.jpg

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

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);

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

此库在 Xamarin.iOS、Xamarin.Android、Xamarin.Forms

上快速轻松地加载图像

确认我这个答案是否适合您!

所以,修复有效,我现在在 phone 中进行测试并且它正常工作,修复在上次编辑中。