Error loading ImageView from InputStream received from web service in Android 3.0 and above
My application needs to display an image preview from image data received as InputStream from a web service. InputStream is decoded to Bitmap for display in ImageView. All this thread works fine in Android 2.2 and 2.3, whereas it doesn't work with Android 3.0 and 4.0. The bitmap decoded in the latter case is null, while its 2.x penalty.
Here is the code snippet that I am using.
ImageView imgView = (ImageView) findViewById(R.id.image);
String imageUrl = "my webservice url";
HttpClient httpClient = new DefaultHttpClient();
HttpGet mHttpGetEntity = new HttpGet(imageUrl);
HttpResponse response;
try {
response = httpClient.execute(mHttpGetEntity);
XmlPullParser xpp = XmlPullParserFactory.newInstance().newPullParser();
InputStream is = response.getEntity().getContent();
xpp.setInput(is, "UTF-8");
// use xpp for process
imgView.setImageBitmap(BitmapFactory.decodeStream(is));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
How can I make this work even with Android 3.0 above. Please advice.
+3
Girish KN
source
to share
1 answer
your code works fine on my 4.0. I would say don't worry about 3.0 as it is an unstable version that no one else will be using.
+2
Win Myo Htet
source
to share