最好的方法从URL在Android的下载图像 [英] Best method to download image from url in Android
问题描述
我用下面的方法来下载网址的单个图像
I'm using below method to download single image from url
public static Bitmap getBitmap(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Bitmap d = BitmapFactory.decodeStream(is);
is.close();
return d;
} catch (Exception e) {
return null;
}
}
有时候,我得到一个内存不足的异常。
Sometimes I get an outofmemory exception.
我无法赶上内存不足的异常。该应用程序将关闭。如何prevent呢?
I am unable to catch outofmemory exception. The app will close. How to prevent this?
是否有下载图像更好的方法也比较快?
Is there a better method for downloading images that is also faster?
推荐答案
我用这个库,它是真正伟大的,当你必须处理大量的图像。它下载他们异步,缓存它们等。
I use this library, it's really great when you have to deal with lots of images. It downloads them asynchronously, caches them etc.
对于OOM异常,使用<一个href="https://$c$c.google.com/p/android-source-browsing/source/browse/samples/training/bitmapfun/src/com/example/android/bitmapfun/util/RecyclingBitmapDrawable.java?spec=svn.platform--development.4d6574e878e2af2f19b46b28b28748ba2786e05a&repo=platform--development&r=4d6574e878e2af2f19b46b28b28748ba2786e05a">this和<一href="https://$c$c.google.com/p/android-source-browsing/source/browse/samples/training/bitmapfun/src/com/example/android/bitmapfun/ui/RecyclingImageView.java?spec=svn.platform--development.4d6574e878e2af2f19b46b28b28748ba2786e05a&repo=platform--development&r=4d6574e878e2af2f19b46b28b28748ba2786e05a">this一流的大幅减少它们对我来说。
As for the OOM exceptions, using this and this class drastically reduced them for me.
这篇关于最好的方法从URL在Android的下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!