【发布时间】:2011-06-10 09:23:00
【问题描述】:
这个让我咬指甲。 我将图像 URL 从 XML 解析为数组列表。我在表格布局中显示图像以及一些文本。我在活动中使用以下代码来显示图像:
位图 bm=DownloadImage(piciterator.next().toString());
icon.setImageBitmap(bm);
这里 piciterator 正在迭代包含 URL 的数组列表。
这里是DownloadImage函数:
私有位图下载图像(字符串 URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
这是 OpenHttpConnection 函数:
私有 InputStream OpenHttpConnection(String urlString) 抛出 IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
在调试时,我可以在浏览器上检索一些图像。然而,在 arraylist 中的第 23 个元素之后,图像在浏览器上打开,但应用程序落在了这一步:
bitmap = BitmapFactory.decodeStream(in);
在浏览器上看到的图像比其他图像要小,但不会小很多。
应用程序甚至没有去尝试捕捉。它只是崩溃。
我们会非常感谢这方面的帮助。
【问题讨论】:
-
为什么不用DroidFu的WebImageView:brainflush.wordpress.com/2009/11/23/…