问题描述
我试图从 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 Opening localstorage
编辑:
嗯...我修复了 Uri 前缀...
我改变了两件事: 在下载图像中,我将其从 Task 更改为 string 并返回一个 string。 代码:
public string DownloadImageString(string URL)
{
WebClient webClient = new WebClient();
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),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 上快速轻松地加载图像
确认我这个答案是否适合你!
,所以,修复有效,我现在正在手机中进行测试,它工作正常,修复在最后一次编辑中。