If I store too many bitmaps (as BLOB) in my sqlite database, while retrieving them back I get strange exception (which I can't catch even as Throwable, but I can see it in LogCat every time):
Failed to read row 2, column 0 from a CursorWindow which has 2 rows, 8 columns
Usually not a good idea to store bitmaps/images in general on your database, it's not efficient at all.
You should save the bitmaps as images and store the paths on the database.
This was discussed on this question.
But... If you're really into storing that for whatever reason, you could also try to encode the image as a Base64
String, and store it on your DB.
Android has the
Base64 class for this. Try to use the following snippet to encode:
ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); // Could be Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.WEBP byte bai = baos.toByteArray(); String base64Image = Base64.encodeToString(bai, Base64.DEFAULT); // Call your method to save this string on the DB here.
And you'll have to decode it try the following:
byte data = Base64.decode(base64Image, Base64.DEFAULT); Bitmap bm; BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inMutable = true; bm = BitmapFactory.decodeByteArray(data, 0, data.length, opt); // Now do whatever you want with the Bitmap.
You can see the docs for the
Bitmap class here.