Karim Aly
Karim Aly

Reputation: 569

BitmapFacotry.decodeByteArray returns null for a valid image's byte array

this is an image in the tag of an mp3 file https://i.sstatic.net/HtXqA.jpg

i get this image byte array from mp3 file with Android's MediaMetaDataRetreiver ... when i try to decode this image using BitmapFactory.decodeByteArray it returns null

Help would be apprecciated

EDIT: when i first decode with options.inJustDecodeBounds = true ... options.outWidth and options.outHeight returns the correct width and height

Full Code:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(context, songUri);
        byte[] data = retriever.getEmbeddedPicture();
        if(data != null)
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(data, 0, data.length, options);
            options.inSampleSize = BitmapUtils.calculateInSampleSize(options, 500, 500);
            options.inJustDecodeBounds = false;
            albumArtBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
        }

Upvotes: 3

Views: 1755

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

Use stream instead:

InputStream is;
    Bitmap bitmap;
    is = context.getResources().openRawResource(R.drawable.someImage);

    bitmap = BitmapFactory.decodeStream(is);
    try {
        is.close();
        is = null;
    } catch (IOException e) {
    }

BTW, I didn't try jpg yet but suppose maybe this is a problem. Try to convert it to "png"

Upvotes: 1

Related Questions