在 Xamarin.Android 中无法在 ImageView 中显示下载的图像

Showing downloaded image in ImageView not working in Xamarin.Android

我有一个小的 png 图像,我想使用 Xamarin.Android 在图像视图中显示。 我正在使用以下代码下载文件:

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(...) returns 该路径为真。所以我确定文件已下载并且存在。

当我想在 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);
}

输出 windows 在应显示图像时显示以下内容:

02-01 23:41:24.770 E/Drawable(19815): Unable to decode stream: android.graphics.ImageDecoder$DecodeException: Failed to create image decoder with message 'unimplemented'Input contained an error. 02-01 23:41:24.770 W/ImageView(19815): resolveUri failed on bad bitmap uri: /data/user/0/myapp/files/hns/hvvstoerungen_facebook.png

但我不明白这到底是什么意思。 还有一件事是:如果我 运行 在全新的 Android 模拟器实例中使用该应用程序,则不会显示此图像和所有其他同类图像。 如果我 运行 旧的 Android 模拟器实例中的应用程序,该应用程序之前已经 运行ning 但在 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");

并将图像保存为 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);
        }