0

I'm trying to convert String into Bitmap object to put it then on ImageView. String value is downloaded from web and is not null. Actually it is a jpeg file which I can download and open via browser.

I tried to use BitmapFactory.decodeByteArray method but got --- SkImageDecoder::Factory returned null message.

try{
      byte[] encodeByte = encodedString.getBytes();
      Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
      return bitmap;
    } catch(Exception e){
       e.printStackTrace();
       return null;
 }

encodedString is actually a string I downloaded before:

...
HttpEntity resEntity = response.getEntity();
String encodedString = EntityUtils.toString(resEntity);
...
Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84

1 Answers1

2

As mentioned in comments, when converting binary data to a string, it changes the data (to valid characgers, goes through encoding) at EntityUtils.toString(resEntity)

Thanks to @yoah

Changed code to

byte[] img = EntityUtils.toByteArray(resEntity);

and then passed this byte array into BitmapFactory

Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);

Image displays correctly.

So, the conclusion is that is better to operate with byte array than with accessory String.

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84