【发布时间】: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 Opening localstorage
编辑:
嗯...我修复了 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