首页 > 解决方案 > SkAndroidCodec::NewFromStream 返回 null System.IO.Stream Xamarin Android

问题描述

我有一个关于 BitmapFactory 的问题。我正在使用 Xamarin,并且正在尝试解码从内部存储中检索到的流,但是每当我尝试使用位图工厂进行解码时,它都不会返回位图。但是,如果我从 URL 中检索完全相同的图像并使用 Bitmap Factory 进行解码,它就可以正常工作。这没有任何意义,请帮助!

public static async Task<Bitmap> HandleContentImage(Post post)
    {

        var charsToRemove = new string[] { "/", ".com", ":", "-", ".", "_", ";" };
        var ContentImageUri = post.image_url;
        foreach (var c in charsToRemove)
        {
            ContentImageUri = ContentImageUri.Replace(c, string.Empty);
        }
        HomeService homeService = new HomeService();
        try
        {
            Stream CachedContentImageStream = await homeService.LoadImageFromCache(ContentImageUri + ".jpg");

            if (CachedContentImageStream != null)
            {
                Bitmap ContentImageBitmap = await BitmapFactory.DecodeStreamAsync(CachedContentImageStream);
                return ContentImageBitmap;
            }
            else
            {
                byte[] ContentImageArray = await homeService.DownloadImageBytesAsync(post.image_url);
                Stream ContentImageStream = new MemoryStream(ContentImageArray);
                await homeService.SaveImageToCache(ContentImageStream, ContentImageUri + ".jpg");
                Bitmap ContentImageBitmap = await BitmapFactory.DecodeStreamAsync(ContentImageStream);

                return ContentImageBitmap;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
            return null;
        }
    }

标签: c#xamarinxamarin.androidbitmapfactory

解决方案


推荐阅读