1

Have a scenario where the httpentity have the binary data of a image in InputStream, for processing further its been converted as String in a library file[String str = EntityUtils.toString(httpResponse.getEntity())] , now am trying to get the input stream back from that String.

Take the below scenario for understanding the issue:

Working - ImageView is displayed with content

InputStream inStream = getContentResolver().openInputStream(thisPhotoUri);
Bitmap bm = BitmapFactory.decodeStream(inStream);
ImageView view = (ImageView)findViewById(R.id.picture_frame);
view.setImageBitmap(bm);

Issue - ImageView is not displayed with image

InputStream inStream = getContentResolver().openInputStream(thisPhotoUri);
String str = inStream.toString();
InputStream is = new ByteArrayInputStream(str.getBytes());
Bitmap bm = BitmapFactory.decodeStream(is);
ImageView view = (ImageView)findViewById(R.id.picture_frame);
view.setImageBitmap(bm);
Raghunandan
  • 129,147
  • 24
  • 216
  • 249
Dinesh
  • 113
  • 2
  • 12

3 Answers3

1

InputStream.toString() does not do, what you're expecting. It will call the Object.toString() method and you will get something like java.io.InputStream@604c9c17, not the real content of the stream!

Try a System.out.println(str); to see, what it's value is.

That's why you can't regenerate the original InputStream from this content, because it is not the content of the InputStream!

You have to read the stream in another way to get the content to the String! See: Read/convert an InputStream to a String

Community
  • 1
  • 1
bobbel
  • 3,031
  • 2
  • 24
  • 41
1

You can't directly convert the InputStream to String. This might be the issue.

String str = inStream.toString();

Take a look at this to identify the way of converting the InputStream to String.

Rakhita
  • 4,293
  • 1
  • 12
  • 15
  • Tried the string convertion as mentioned in the link, when i convert the string back to stream & pass it to bitmap factory it still return null. – Dinesh Feb 20 '14 at 10:47
  • Why you want to convert input stream to string, are you doing any modification to it? – Rakhita Feb 20 '14 at 11:50
  • Http calls are done inside a jar file and the response is given back as a json, so in this case the stream(image binary) is converted as string and added to json, the application layer is now trying to use the string to get the stream back & get the image. – Dinesh Feb 20 '14 at 17:25
-1

This should be what you are looking for:

InputStream stream = new ByteArrayInputStream(yourString.getBytes("UTF-8"));

Tomasz Kryński
  • 373
  • 3
  • 19