在ImageView中显示下载的图像在Xamarin.Android中不起作用
我有一个小png图像,我喜欢用Xamarin.Android在imageview中显示。 我正在使用以下代码下载该文件:在ImageView中显示下载的图像在Xamarin.Android中不起作用,xamarin.android,Xamarin.android,我有一个小png图像,我喜欢用Xamarin.Android在imageview中显示。 我正在使用以下代码下载该文件: private void Download() { var url = "https://hns.d7u.de/v4/images/hvvstoerungen_facebook.png"; var directory = Environment.GetFolderPath(Environment.SpecialFolder.Personal
private void Download()
{
var url = "https://hns.d7u.de/v4/images/hvvstoerungen_facebook.png";
var directory = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/myapp/";
var fileName = url.Substring(url.LastIndexOf("/") +1);
var path = directory + fileName;
System.Net.WebClient wC = new System.Net.WebClient();
wC.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding, "gzip");
wC.DownloadDataCompleted += WC_DownloadDataCompleted;
wC.DownloadDataAsync(new Uri(url), path);
}
private void WC_DownloadDataCompleted(object sender, System.Net.DownloadDataCompletedEventArgs e)
{
var path = e.UserState.ToString();
var bytes = e.Result;
if (File.Exists(path))
File.Delete(path);
if (!File.Exists(path))
File.WriteAllBytes(path, bytes);
}
它存储在/data/user/0/myapp/files/hns/hvstoerungen_facebook.png,一个File.Exists(…)为该路径返回一个true。因此,我确信,该文件已下载且存在
当我想在ImageView中显示它时,我会这样做:
if (System.IO.File.Exists(imageFilePath))
{
Android.Net.Uri andrUri = Android.Net.Uri.Parse(imageFilePath);
ImageIcon.SetImageURI(andrUri);
//Also not working:
//Bitmap bitmap = BitmapFactory.DecodeFile(imageFilePath);
//ImageIcon.SetImageBitmap(bitmap);
//And also not working:
//Android.Net.Uri andrUri = Android.Net.Uri.Parse(imageFilePath);
//Bitmap bmp = BitmapFactory.DecodeStream(Android.App.Application.Context.ContentResolver.OpenInputStream(andrUri));
//ImageIcon.SetImageBitmap(bmp);
}
当应显示图像时,输出窗口显示以下内容:
02-01 23:41:24.770 E/可绘制(19815):无法解码流:
android.graphics.ImageDecoder$DecodeException:无法创建图像
消息“未实现”输入的解码器包含错误。02-01
23:41:24.770 W/ImageView(19815):错误位图uri上的resolveUri失败:
/data/user/0/myapp/files/hns/hvstoerungen_facebook.png
但我不明白这到底意味着什么。
另外一件事是:如果我在全新的Android Emulator实例中运行该应用程序,则不会显示此图像和所有其他类型的图像。
如果我在旧的Android Emulator实例中运行该应用程序,该应用程序以前已经运行过,但基于Android.Forms,则会显示旧项目已知的旧图像,而不会显示新下载的图像。所有图像都在同一个文件夹中,我看不出它们之间有任何差异
有人有主意吗
编辑:
我的工作版本改为使用以下Download()方法:
private void Download()
{
var noCompression = new string[] { ".png", ".jpg", ".jpeg", ".gif", ".zip", ".7z", ".mp3", ".mp4" };
var url = "https://hns.d7u.de/v4/images/hvvstoerungen_facebook.png";
var directory = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/myapp/";
var fileName = url.Substring(url.LastIndexOf("/") +1);
var path = directory + fileName;
System.Net.WebClient wC = new System.Net.WebClient();
if (!noCompression.Contains(url.Substring(url.LastIndexOf('.'))))
wC.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding, "gzip");
wC.DownloadDataCompleted += WC_DownloadDataCompleted;
wC.DownloadDataAsync(new Uri(url), path);
}
您可以尝试下面的代码 从Url下载图像:
public Bitmap GetImageBitmapFromUrl(string url)
{
Bitmap imageBitmap = null;
using (var webClient = new WebClient())
{
var imageBytes = webClient.DownloadData(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}
}
return imageBitmap;
}
用法:
bitmap = GetImageBitmapFromUrl("https://hns.d7u.de/v4/images/hvvstoerungen_facebook.png");
ExportBitmapAsPNG(bitmap);
并将图像另存为png:
void ExportBitmapAsPNG(Bitmap bitmap)
{
var folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
filePath = System.IO.Path.Combine(folderPath, "test.png");
var stream = new FileStream(filePath, FileMode.Create);
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
stream.Close();
}
用法:
bitmap = GetImageBitmapFromUrl("https://hns.d7u.de/v4/images/hvvstoerungen_facebook.png");
ExportBitmapAsPNG(bitmap);
检查文件是否存在并设置到imageview中:
if (File.Exists(filePath))
{
Bitmap myBitmap = BitmapFactory.DecodeFile(filePath);
imageview.SetImageBitmap(myBitmap);
}
谢谢你,这让一切都不同了!别忘了像我一样删除当前存在的文件。事实上,我也发现了我原始代码中的问题所在:gzip编码的头。如果我删除它,问题中的代码也会起作用。注意:问题是这一行:
wC.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding,“gzip”)代码>。如果我删除它,图像显示没有任何问题。