A subproblem in my app is that I have to post an image to a server, where it is further processed. I am currently sending images by encoding them (jpeg compressed) into a base64 string, and then posting as json. However, this led to erroneous results. On debugging, I realized that the base64 representation of image formed by android was incorrect. (I tested this by comparing with the base64 representation of the image made by the base64 utility in linux) .
My code to get a base64 representation of an image is as follows
// get base64 encoded image from bitmap
public String getEncodedImage(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;
}
I am passing a bitmap to the above function, which is obtained as
final Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
Can someone suggest a reason why this code might lead to an incorrect base64 representation for some images?
Thanks in advance.