I was trying to save an image from an URL and I have an inner exception inside an inner exception tellin the URi prefix is not recognized.
Full exception:
[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.
Some code:
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);
}
EDIT: (forgot to add this) (NOT REAL URL) The URL is https://api.com/imagenes/partes/18005/2021062311191329243400.jpg
EDIT: (answering Jason, making sure if URL is correct on webClient)
Screenshot Opening localstorage
EDIT:
Well... I fixed the Uri prefix...
I changed 2 things: In the Download Image I changed it from Task to string and returning a string. Code:
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;
}
And then I changed the way I was sending the URL.
Instead of sending a new Uri(url) I'm sending a string. That string is mounted manually again, not by taking imagen.Source (this was the real fix) Code:
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);
So, the fix works, I was testing in the phone now and it works correctly, the fix is in the last edit.