我的应用程序需要从网络服务获取的InputStream中获取图像数据,并显示图像预览。 InputStream被解码为位图以在ImageView中显示。整个流程在Android 2.2和2.3中运行良好,但在Android 3.0和4.0中无法正常工作。在后者中解码的位图为空,而在2.x中却没有问题。
这是我使用的代码片段:
这是我使用的代码片段:
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();
}
如何使此功能在 Android 3.0 及以上版本中工作?请给出建议。