我正在研究从url下载图像的应用程序。问题是只有一些图像被正确下载,而另一些则没有。 首先,现在的问题是代码:Android:下载图像并转换为位图时出现问题
public Bitmap downloadImage(String url) {
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try {
response = client.execute(new HttpGet(url));
} catch (ClientProtocolException cpe) {
Log.i(LOG_FILE, "client protocol exception");
return null;
} catch (IOException ioe) {
Log.i(LOG_FILE, "IOE downloading image");
return null;
} catch (Exception e) {
Log.i(LOG_FILE, "Other exception downloading image");
return null;
}
// Convert images from stream to bitmap object
try {
Bitmap image = BitmapFactory.decodeStream(response.getEntity().getContent());
if(image==null)
Log.i(LOG_FILE, "image conversion failed");
return image;
} catch (Exception e) {
Log.i(LOG_FILE, "Other exception while converting image");
return null;
}
}
所以我有什么是需要的URL作为一个字符串参数,然后下载图像的方法,由BitmapFactory的手段获取HttpResponse流转换为位图.decodeStream方法,并返回它。问题是,当我在慢速网络连接(几乎总是3G而不是Wi-Fi)时,一些图像被转换为空 - 不是全部,只有其中的一部分。使用Wi-Fi连接完美工作;所有的图像都被正确下载和转换。
有谁知道为什么会发生这种情况?或者更好,我该如何解决这个问题?我怎么会去测试以确定问题呢?任何帮助都很棒;谢谢!
什么是m和n是什么?我在理解代码示例时遇到了一些问题,也许一些更长的变量名会有所帮助,也许会有一两个评论。此外,您是否有链接到这个问题上的错误请求,或者这是一个普遍的问题,在未来版本的JPEG解码器无法解决? – Janusz 2010-06-16 08:28:33
非常感谢罗曼·盖伊!没有你的帮助,我永远都不会想到这件事。 对于遇到此问题的其他人,Romain Guy提到的第一个解决方案可以在他的这一课程中找到:http://code.google.com/p/shelves/source/browse/trunk/Shelves/src/组织/ curiouscreature /安卓/货架/ UTIL/ImageUtilities.java – Mike 2010-06-16 21:18:37