我对这个错误有疑问。
我从 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 个图标......
我想,你会用通用图像加载器 http:////%20Load%20image,%20decode%20it%20to%20Bitmap%20and%20return%20Bitmap%20synchronously%20ImageSize%20targetSize%20=%20new%20ImageSize(80,%2050);%20//%20result%20Bitmap%20will%20be%20fit%20to%20this%20size%20Bitmap%20bmp%20=%20imageLoader.loadImageSync(imageUri,%20targetSize,%20options);
给定代码片段的方法
// 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)