How to display image from blob data in android sqlite?

I am trying to save android form form sdk in sqlite database. And everything turned out fine. The image will be stored in the database as a blob. This is the crude code I used for this.


Bitmap bitmap = BitmapFactory.decodeFile(filePath); imgView.setImageBitmap(bitmap); int size = bitmap.getWidth() * bitmap.getHeight(); ByteArrayOutputStream out = new ByteArrayOutputStream(size); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace();} byte[] x = out.toByteArray(); 

On the database side, the code is as follows

  ContentValues values = new ContentValues(); byte[] bytes = null; bytes = img_bytes.getBytes(); values.put("IMAGE", bytes); 

And for display, I used the code as shown below.

  ImageView image = new ImageView(this); byte[] img_bytes = result.get("IMAGE").getBytes(); Bitmap bMap = BitmapFactory.decodeByteArray(img_bytes, 0, img_bytes.length); image.setImageBitmap(bMap); 

When I printed the string value of the byte array, it turned out like [B @ 44da2db0. But the byte array is not displayed in the image.

Someone please help me with this. Thanks

0
source share
1 answer

I have found a solution. Thanks to everyone.

I used the base64 encoding method. i.e; To display the image, I used a hash map to get the result primarily from the database table. Therefore, before saving to the map, I encoded the bytes in the string using the base 64 method. Then, on the Activity side, to display the image, I again decoded the string using the base 64, and then converted it to bytes and the image was displayed.

+3
source

All Articles