25
votes

I want to convert base64 encoded string into bitmap so i can put it in image view, but getting error like

D/skia(7490): --- decoder->decode returned false and bitmap returns null value

My code is:

byte[] imageAsBytes = Base64.decode(imageData);

image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
6
Where did you get your base64 string? Are you sure it is valid? - draksia
i am getting base64 string from webservice. How we can check it is valid or not? - Baskar
Can you post the code that gets the Base64 string? - draksia
ImageData is a base64encoded string only. size of the string length is same only what in webservice and what i am getting to device, but i can't able to show in imageview. - Baskar

6 Answers

51
votes

Firts you have to check that the string you want to decode is vaild and has the intended value to be decoded and to do so, you can do something like below:

filePath= Environment.getExternalStorageDirectory()
                        + "/SaudiScore/temporary_holder.jpg";
Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
String strBase64=Base64.encodeToString(byteArray, 0);

then you can decode the string that you just encoded and get the image back by doing something like the following:

byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
image.setImageBitmap(decodedByte);
24
votes
byte[] decodedString = Base64.decode(mBase64string, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
mImageView.setImageBitmap(decodedByte);
7
votes
String base = "Base64 string values of some image";

byte[] imageAsBytes = Base64.decode(base.getBytes(), Base64.DEFAULT);

ImageView image = (ImageView) this.findViewById(R.id.imageView1);

image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));

Try this code.

0
votes

This method can help:

private void setExistImage(ImageView imageView, String base64String){
    if (!base64String.isEmpty()) {
        byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
        imageView.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
    }
}
0
votes

Decode/Convert base64 string to image

    imageBytes = Base64.decode(imageString, Base64.DEFAULT);
    Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0,imageBytes.length);
    image.setImageBitmap(decodedImage);
0
votes
 byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
image.setImageBitmap(decodedByte);

I was using the above solution .It always returned errors like decoded String is null,IllegalStateException .. All I did was I just wrapped that in a try catch