0

This is my code and I tried lot of code varieties already...

public String getStringImage(Bitmap bmp){ 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] imageBytes = baos.toByteArray(); 
    String encodedImage = Base64.encodeToString(imageBytes,Base64.DEFAULT); 
    return encodedImage; 
}

Bryan
  • 14,756
  • 10
  • 70
  • 125
Sayandh
  • 61
  • 1
  • 10

1 Answers1

0

You can try following code to encode and decode image:

//Compress img and save
private String encodeBitmapAndSave(Bitmap bitmap1) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
}

//Decompress img 
private Bitmap decodeFromFirebaseBase64(String image) throws IOException {
    byte[] decodedByteArray = Base64.decode(image, Base64.DEFAULT);
    return BitmapFactory.decodeByteArray(decodedByteArray, 0, decodedByteArray.length);
}
Shruti
  • 803
  • 9
  • 26
  • yup !but you didn't post how you decoding it. And make sure your Bitmap is not empty. – Shruti Jul 17 '17 at 05:57
  • I have checked bitmap is ok its displaying correctly.. But the encoded string is not getting well formatted – Sayandh Jul 17 '17 at 06:00
  • hello.... The encoding is correct , i can decode that image from same activity and gets image back... But i need to upload this data string into my sql ... but when uploaded if the original image size is less than 100 kb, But it shows around 1mb in mysql.. Can u please help. – Sayandh Jul 17 '17 at 06:34
  • @Sayandh you must be doing something wrong while saving Base64 into mysql. You should use `blobe` type for it as shown [here](https://stackoverflow.com/a/11790199/2404262) – Anshul Tyagi Jul 17 '17 at 07:08
  • The data type on database is currect is medium blob... and i am sending the data as string to the php page using nameValuepair... – Sayandh Jul 17 '17 at 07:24
  • @Shruti hei can i get complete code for upload image to mysql from android using php... I think the problem is on php code.. Is it need to decode base64 to upload image as blob on mysql.. Please reply.. – Sayandh Jul 17 '17 at 11:42
  • @Sayandh I haven't used php to upload image; will check and tell you in some time. – Shruti Jul 17 '17 at 12:07
  • @Shruti thanks.. is there any other good option to upload image into mysql database.. – Sayandh Jul 17 '17 at 12:10
  • @Sayandh check if [this](https://stackoverflow.com/questions/36766751/how-to-upload-long-blob-image-to-mysql-database-using-java-and-retrieve-in-php) link works – Shruti Jul 17 '17 at 12:18
  • @Shruti no its not worked... I didnt get any working methode to store image in mysql database.. Still searching... – Sayandh Jul 19 '17 at 04:33