【问题标题】:Bitmap OutOfMemoryError位图 OutOfMemoryError
【发布时间】:2023-04-09 03:09:01
【问题描述】:

我对这个错误有疑问。

我从 URL 制作网站图标解析器。我这样做:

public class GrabIconsFromWebPage {
public static String replaceUrl(String url) {
    StringBuffer sb = new StringBuffer();
    Pattern p = Pattern.compile("https?://.+\\..+?\\/");
    Matcher m = p.matcher(url);
    while (m.find()) {
        sb.append(m.group());
    }
    return sb.toString();
}

public static String getFavicon(String url) throws IOException {
    try {
        Document doc = Jsoup.connect(url).get();
        Element element = doc.head().select("link[href~=.*\\.(ico|png)]").first();
        if (element != null) {
            if (element.attr("href").substring(0, 2).contains("//")) {
                return "http:" + element.attr("href");
            } else if (element.attr("href").substring(0, 4).contains("http")) {
                return element.attr("href");
            } else {
                return replaceUrl(url) + element.attr("href");
            }
        } else {
            return "";
        }
    } catch(IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch(OutOfMemoryError er) {
        er.printStackTrace();
    }
    return "";
}

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

}

以及如何从 url 获取位图

Bitmap faviconBitmap = GrabIconsFromWebPage.getBitmapFromURL(
                                GrabIconsFromWebPage.getFavicon(
                                        bookmarkData.get(position).getUrl() // url from which I want to grab favicon
                                )
                        );

上传 20 张图片后的这段代码给了我 OutOfMemoryError。我怎样才能解决这个问题?还是优化?因为在我显示此图标的列表中,可以有超过 20 或 40 个图标...

【问题讨论】:

  • 你能发一个MCVE 让我们可以运行吗?
  • 您能否告诉我们以下内容:1) 分配的内存 2) 您对获取的 URL 做了什么?
  • @MJSG,1. 标准内存量,2. 我把它给了解析器,如果找到它们,他就会得到图标。我发布了更多详细信息代码
  • 你如何处理创建的Bitmap对象,它是否存储在内存中?如果是,那么这很可能是你的罪魁祸首。
  • @KevinWorkman,是的,也许,如果有帮助的话

标签: java android html parsing


【解决方案1】:

我想,你会使用universal image loader

给定的方法sn-p

// Load image, decode it to Bitmap and return Bitmap synchronously 
ImageSize targetSize = new ImageSize(80, 50); 
// result Bitmap will be fit to this size
Bitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, options);

如果内存不足,您可以在清单文件中添加一行

<application
        ...
        android:largeHeap="true"
        ...
        >
</application>

【讨论】:

  • 不应仅仅因为内存不足而使用大堆。 developer.android.com/training/articles/…
  • 是的,它影响了您的应用程序和您需要的图像处理水平。
  • 有帮助)但应用程序在启动超过 2 分钟后加载)
  • 好的,你也可以试试通用图片加载器中的其他选项。
【解决方案2】:

自己解析图标是个坏主意。谷歌在我们之前就做​​到了

http://www.google.com/s2/favicons?domain=(domain)

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 2014-06-21
    • 2012-11-30
    相关资源
    最近更新 更多