【问题标题】:Error on displaying image in imageview在 imageview 中显示图像时出错
【发布时间】: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);

在浏览器上看到的图像比其他图像要小,但不会小很多。

应用程序甚至没有去尝试捕捉。它只是崩溃。

我们会非常感谢这方面的帮助。

【问题讨论】:

标签: android imageview


【解决方案1】:

它只是崩溃而不打印堆栈跟踪的事实暗示了潜在的内存不足问题。

IIRC,Android 应用的大小限制为 16MB。您是否验证了图片的组合大小是否超过了此限制?

【讨论】:

    【解决方案2】:

    始终尝试在网络连接中使用Thread

    这是文档。

    Painless Treading

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2015-11-09
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      相关资源
      最近更新 更多