在ImageView中显示下载的图像,在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) + "/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/hvvstoerungen_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/Drawable(19815):无法解码流:android.graphics。ImageDecoder$DecodeException:创建图像失败消息"未实现"的解码器输入包含错误。02-01[23:41:24.770] W/ImageView(19815): resolveUri处理错误的位图uri失败/数据/user/0/myapp/文件/环/hvvstoerungen_facebook.png

但是我不明白这到底是什么意思。另外一件事是:如果我在一个全新的Android Emulator实例中运行该应用程序,则不会显示此图像和所有其他类型的图像。如果我在旧的Android模拟器实例中运行应用程序,该应用程序之前已经运行,但在Android上。表单基础上,显示旧项目已知的旧图像,而不显示新下载的图像。所有的图片都在同一个文件夹里,我看不出它们之间有什么区别。

有人有主意吗?

编辑:我的工作版本有以下下载()方法代替:

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

保存图片为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();
}

用法:

ExportBitmapAsPNG(bitmap);     

检查文件是否存在并设置到imageview:

if (File.Exists(filePath))
{               
Bitmap myBitmap = BitmapFactory.DecodeFile(filePath);
imageview.SetImageBitmap(myBitmap);
}

最新更新