137

I want to store image in SQLite DataBase. I tried to store it using BLOB and String, in both cases it store the image and can retrieve it but when i convert it to Bitmap using BitmapFactory.decodeByteArray(...) it return null.

I have used this code, but it returns null

Bitmap  bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
2
  • 4
    Please read the first 5-10 links in the "Related" section on this page. Commented Oct 1, 2011 at 13:11
  • 2
    Did you encode the bitmap before writing to database? Commented Oct 1, 2011 at 14:04

3 Answers 3

311

Just try this:

Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.jpg");
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* Ignored for PNGs */, blob);
byte[] bitmapdata = blob.toByteArray();

If bitmapdata is the byte array then getting Bitmap is done like this:

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

Returns the decoded Bitmap, or null if the image could not be decoded.

Sign up to request clarification or add additional context in comments.

3 Comments

image could not be decoded if it is in other format that you are trying to decode from
What if I need to perform such an operation many times in sequence? Isn't it resource-consuming to create new Bitmap object every time? Can I somehow decode my array into existing bitmap?
I post a different answer when you just have a buffer of the image pixel. I was getting always null because of the lack of with, height and color in my buffer. Hope it helps!
39

The answer of Uttam didnt work for me. I just got null when I do:

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

In my case, bitmapdata only has the buffer of the pixels, so it is imposible for the function decodeByteArray to guess which the width, the height and the color bits use. So I tried this and it worked:

//Create bitmap with width, height, and 4 bytes color (RGBA)    
Bitmap bmp = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
ByteBuffer buffer = ByteBuffer.wrap(bitmapdata);
bmp.copyPixelsFromBuffer(buffer);

Check https://developer.android.com/reference/android/graphics/Bitmap.Config.html for different color options

2 Comments

what is mBitmaps?
@Julian How to byte[] cannot be cast to java.lang.String when retreving image from Sqlite stackoverflow.com/questions/63658886/…
0

Kotlin version of Uttam's answer which I used for my app. The uri can be obtained, for example, using registerForActivityResult (see OnActivityResult method is deprecated, what is the alternative? answer of Muntashir Akon)

var uri: Uri? = null
var bitmap: Bitmap? = null

// enter the code to get the uri

try {
    val source = ImageDecoder.createSource(
        context.contentResolver,
        uri!!
    )
    bitmap = ImageDecoder.decodeBitmap(source) { decoder, _, _ ->
        decoder.setTargetSampleSize(1) // shrinking by
        decoder.isMutableRequired = true // this resolve the hardware type of bitmap problem
    }
} catch (e: Exception) {
    e.printStackTrace()
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.