27

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));
Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
Baskar
  • 549
  • 2
  • 6
  • 12

6 Answers6

52

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);
Husam A. Al-ahmadi
  • 2,056
  • 2
  • 20
  • 27
  • Actually I am getting encode string, sending to webservice and back to device, that time i am getting this error. – Baskar Mar 28 '13 at 13:42
  • 1
    I got the same error before, I was facing the same problem, All what you have to do is to check the size of string which actually was an image that encoded before you save it into the remote database is as same as the size of string that you retrieved, once you got it exactly the same you will get the image using the code above. – Husam A. Al-ahmadi Mar 28 '13 at 13:49
  • I am comparing that string length and size also same only, but i can't able to show in imageview. – Baskar Mar 28 '13 at 14:08
  • what is the type of data you used to save the string on the database? – Husam A. Al-ahmadi Mar 28 '13 at 14:12
  • also how did you post the string to be save in remote database? – Husam A. Al-ahmadi Mar 28 '13 at 14:13
  • 1
    I am not saving to database, directly using as a string. For Sending also string only. I need to save in to database ? – Baskar Mar 28 '13 at 14:20
28
byte[] decodedString = Base64.decode(mBase64string, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
mImageView.setImageBitmap(decodedByte);
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Saurabh Bhandari
  • 340
  • 3
  • 11
7
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.

JerabekJakub
  • 5,268
  • 4
  • 26
  • 33
Nikhil Dinesh
  • 3,359
  • 2
  • 38
  • 41
0

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));
    }
}
Hadi Note
  • 1,386
  • 17
  • 16
0

Decode/Convert base64 string to image

    imageBytes = Base64.decode(imageString, Base64.DEFAULT);
    Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0,imageBytes.length);
    image.setImageBitmap(decodedImage);
Aftab Alam
  • 1,969
  • 17
  • 17
0
 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

Abraham Mathew
  • 2,029
  • 3
  • 21
  • 42