0

I have encoded the imageview using base64 and passed that value to textview as hidden as i have successfully inserted into db. my code to encode:

public void convertImg(){
    imageView.buildDrawingCache();
    Bitmap bm = imageView.getDrawingCache();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
    byte[] b = baos.toByteArray();
    String encodedImage = Base64.encodeToString(b , Base64.DEFAULT);
    base64.setText(encodedImage);
}

This is what im getting in my mysql db. is it correct? enter image description here My question is how to decode it and where to see/view it?

Marco Polo
  • 23
  • 10

3 Answers3

1

you have to implement BitmapFactory.decodeByteArray() for decode your image base64 string have look this.

//decode base64 string to image
imageBytes = Base64.decode(encodedImage , Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
mage.setImageBitmap(decodedImage);

Hope it will help you!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
1

Your image encoding looks right. To decode encoded string back to image you need to implement BitmapFactory.

First get bytes from the encoded string. Then use BitmapFactory to decode the byte array. BitmapFactory.decodeByteArray returns a bitmap that you then can use in an imageView.

byte[] b = Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b,0,b.length);
imageView.setImageBitmap(bitmap);
Speditz
  • 171
  • 1
  • 8
  • can i write php code to decode and view it in my localhost web browser? – Marco Polo Jun 07 '18 at 05:58
  • 1
    I personally don't know, take a quick look into [this thread](https://stackoverflow.com/a/15153931/6623419), you might find it helpful. – Speditz Jun 07 '18 at 06:02
0

Since this was tagged Android I assume you're referring to sqlite?

Not a direct answer to your question but in sqlite you can store the binary blob without conversion to base64 first. It may be more efficient in your case since converting to/from base64 has a cost.

https://www.sqlite.org/datatype3.html

Jeremy
  • 555
  • 2
  • 4
  • 10