Converting base64 imageString to bitmap to display in ImageView showing null in bitmap

As you can see from the logs that

E/myBitmap: null

This means you are not able decode bitmap. Please refer this answer

Convert Base64 string to bitmap

Also make sure that you are taking appropriate approach depending upon whether or not you are receiving

data:image/jpg;base64

in the base64 encoded string. That is either slice the part

data:image/jpg;base64

from the string then decode it using Base64.DEFAULT

or alternatively use Base64.URL_SAFE if you are slicing it out.

Edit: Change the following line in loadImage

Bitmap myBitmap = BitmapFactory.decodeStream(input);

to

BufferedReader in = new BufferedReader(new InputStreamReader(input));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();

String encodedImage = response.toString();
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap myBitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

Also if it doesn't work let me know what the value of encodedImage string is. Also make sure that

data:image/jpg;base64

is not being passed in the response.

If you are having problem verifying whether the string is image you can create a simple html page with following

<!DOCTYPE html>
<html>
<head>
    <title>Base 64 Sample</title>
</head>
<body>

<img src="data:image/< png/jpeg/gif etc goes here >;base64,< image content goes here>">

</body>
</html>

replace content within '<' , '>' with appropriate content and open html page in browser. Alternatively you can head over to this link and paste the string there.


You are getting correct base64 encoded String. Just convert that String into bitmap using following code:

byte[] decodedString = Base64.decode(StrBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
imageView1.setImageBitmap(decodedByte);

Bitmap that you are passing to bitMapToString function is null so you are getting null object error.

You are not getting correct bitmap in loadImage(); so make changes like

InputStream input = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
Bitmap myBitmap = BitmapFactory.decodeStream(input);

In this way you will get bitmap