Android/Java - How to retrieve image from web service and show it
<p>I'm using some services of a web service which usually returns JSON respones, but one service when I send the ID of a user returns a static GIF image (Not animated).</p>

<p>The procedure that I'm doing is:</p>

<p>1.Connect to web service using DefaultHttpClient<br>
2.Convert the InputStream received to a String with this utilitary method:</p>

<pre><code>public static String inputStreamToStringScanner(InputStream in) {

  Scanner fileScanner = new Scanner(in);
  StringBuilder inputStreamString = new StringBuilder();
  while(fileScanner.hasNextLine())
    inputStreamString.append(fileScanner.nextLine()).append("\n");
  fileScanner.close();

  return inputStreamString.toString();
}
</code></pre>

<p>3.Storage the converted received String to process the server response.</p>

<p>For the Image Service, when I see the string converted, it begins like this:
"GIF89a?��?�� ..."</p>

<p>It's a static GIF file.</p>

<p>I'm not being able to show the image in a ImageView, I have tried different things that I found in the web:</p>

<pre><code>public void onPhotoFinished (String responseData) {

  InputStream is = new ByteArrayInputStream(responseData.getBytes());
  Bitmap bm = BitmapFactory.decodeStream(is);
  userImage.setImageBitmap(bm);
}
</code></pre>

<p>This is something else that I have also tried:</p>

<pre><code>public void onPhotoFinished (String responseData) {

  InputStream is = new ByteArrayInputStream(responseData.getBytes());
  final Bitmap bm = BitmapFactory.decodeStream(new BufferedInputStream(is));
  userImage.setImageBitmap(bm);
}
</code></pre>

<p>This also doesn't works:</p>

<pre><code>public void onPhotoFinished (String responseData) {

  InputStream is = new ByteArrayInputStream(responseData.getBytes());
  Drawable d = Drawable.createFromStream(is, "src name");
  userImage.setImageDrawable(d);
}
</code></pre>

<p>Finally, this doesn't work either:</p>

<pre><code>public void onPhotoFinished (String responseData) {

  Bitmap bm = BitmapFactory.decodeByteArray(responseData.getBytes(), 0, responseData.getBytes().length);
  userImage.setImageBitmap(bm);
}
</code></pre>

<p>In Logcat I receive "decoder->decode returned false"</p>

<p>Nothing seems to work... any ideas of what is wrong?</p>

<p>Thanks!</p>
