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; 
}

8
  • Any body got the answers....?? Commented Jul 17, 2017 at 5:38
  • 1
    Please refer this link enter link description here Commented Jul 17, 2017 at 5:40
  • Yea sir I have tried that too... But result is same.. The conversion is taking place but i think it is not well formatted.. Commented Jul 17, 2017 at 5:45
  • what do you need Base64.encodeToString for? why cannot you use raw bytes instead? Commented Jul 17, 2017 at 6:24
  • i need to upload this file into mysql.. Commented Jul 17, 2017 at 6:35

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

10 Comments

yup !but you didn't post how you decoding it. And make sure your Bitmap is not empty.
I have checked bitmap is ok its displaying correctly.. But the encoded string is not getting well formatted
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 you must be doing something wrong while saving Base64 into mysql. You should use blobe type for it as shown here
The data type on database is currect is medium blob... and i am sending the data as string to the php page using nameValuepair...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.