【问题标题】:Xamarin Android. Transform Byte array to Bitmap. Skia Decoder returns falseXamarin 安卓。将字节数组转换为位图。 Skia 解码器返回 false
【发布时间】:2014-05-05 14:25:03
【问题描述】:

尝试将存储在 SQLite 数据库中的一些图像作为 blob 转换为位图时出现以下错误。

[skia] --- decoder->decode returned false

我正在尝试以下代码:

// Loads a Bitmap from a byte array
public static Bitmap bytesToBitmap (byte[] imageBytes)
{
    Bitmap bitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);

    return bitmap;
}

结果:一些图像被成功转换,但其他图像得到 skia decode returned false。总是显示相同的图像,而其他相同的图像会出现错误。

在 iOS 应用上使用相同的数据库,并且所有图像都正确显示。图片为 jpeg。

我发现类似问题已解决 here,但我无法将其翻译成 C#。

有没有人知道从字节数组加载位图而不会出现此类问题的解决方法?

【问题讨论】:

    标签: bitmap xamarin.android xamarin blob skia


    【解决方案1】:

    终于搞定了!!!

    我不得不做出这样的解决方法:

    /// Loads a Bitmap from a byte array
    
    public static Bitmap bytesToUIImage (byte[] bytes)
    {
        if (bytes == null)
            return null;
    
        Bitmap bitmap;
    
    
        var documentsFolder = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
    
        //Create a folder for the images if not exists
        System.IO.Directory.CreateDirectory(System.IO.Path.Combine (documentsFolder, "images"));
    
        string imatge = System.IO.Path.Combine (documents, "images", "image.jpg");
    
    
        System.IO.File.WriteAllBytes(imatge, bytes.Concat(new Byte[]{(byte)0xD9}).ToArray());
    
        bitmap = BitmapFactory.DecodeFile(imatge);
    
        return bitmap;
    }
    

    请注意,创建的文件缺少 .jpeg 文件“D9”的结束字节,因此我必须手动添加它。我知道事实上我的图像包含了这个字节,我还尝试通过使用 BitmapFactory.DecodeByteArray 将“D9”添加到 byteArray 来生成位图,但它没有用。

    所以,唯一对我有用的解决方法是从 byteArray 创建一个文件并解码该文件。希望它可以帮助将来的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 2010-09-26
      • 1970-01-01
      • 2015-06-04
      • 2018-05-06
      相关资源
      最近更新 更多