Question

I am getting the following error when trying to transform some images stored in a SQLite database as blob into bitmap.

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

I am trying the following code:

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

    return bitmap;
}

Result: Some images are being transformed successfully but others get skia decode returned false. Always the same images are shown and the same others get the error.

The same database is used on an iOS app and all the images are shown properly. The images are jpeg.

I've found similar problems resolved here, but I weren't able to translate it into C#.

Does anyone know a workaround to load bitmaps from byte arrays without these kind of problems?

Was it helpful?

Solution

Finally got it working!!!

I had to make a workaround like this:

/// 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;
}

Note that the file created was missing the ending byte of a .jpeg file "D9" so I had to add it manually. I know for fact that my images had this byte included, and I also tried to generate the bitmap by adding "D9" to the byteArray with BitmapFactory.DecodeByteArray but it didn't work.

So, the only workaround that works for me is creating a file from the byteArray and decoding that file. Hope it could help someone in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top