当前位置: 代码迷 >> Android >> Android 大量图片加载,使用什么加载库,避免OOM,该如何解决
  详细解决方案

Android 大量图片加载,使用什么加载库,避免OOM,该如何解决

热度:99   发布时间:2016-04-28 03:48:45.0
Android 大量图片加载,使用什么加载库,避免OOM
我现在用的是Universal Image Loader,全局init以及配置config,但还是有OOM的问题出现
以下是这些配置的代码段
MyApplication.java
ImageLoaderConfiguration config = ImageLoaderConfigurationUtil.getCustomImageLoaderConfiguration(getApplicationContext());
ImageLoader.getInstance().init(config);

ImageLoaderConfigurationUtil.java
public class ImageLoaderConfigurationUtil {

public static ImageLoaderConfiguration getCustomImageLoaderConfiguration(Context context) {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).memoryCacheExtraOptions(640, 640)
// default = device screen dimensions
.threadPoolSize(5)
// default
.threadPriority(Thread.MAX_PRIORITY - 1)
// default
.tasksProcessingOrder(QueueProcessingType.FIFO)
// default
// memoryCache
.memoryCache(new UsingFreqLimitedMemoryCache(16 * 1024 * 1024))
// memoryCacheSizePercentage
.memoryCacheSizePercentage(12)
// default
.diskCacheSize(50 * 1024 * 1024)
// diskCacheFileCount
.diskCacheFileCount(100)
// cacheFileNameGenerator
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(context)) // default
.imageDecoder(new BaseImageDecoder(true)) // default
// logs,delete it when release
.writeDebugLogs()
// build
.build();
return config;
}
}


DisplayImageOptionsUtil.java
public static DisplayImageOptions getDefaultOptions() {

DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.default_img).showImageForEmptyUri(R.drawable.default_img)
.showImageOnFail(R.drawable.default_img).resetViewBeforeLoading(false).cacheOnDisk(true).imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.bitmapConfig(Bitmap.Config.RGB_565).displayer(new FadeInBitmapDisplayer(0)).build();
return options;
}


现在的情况是这样,使用这个库在瀑布流里面加载图片,程序运行到一定时间后,开始出现OOM报错,程序虽然没有被FC,但是很多图片开始加载不出来,程序也明显的卡顿,想问一下大家,我这样的配置和使用方式是对的么,还有使用这个类库应该注意一些什么问题,要怎样及时回收掉不用的缓存?
又或者大家有什么好的异步图片加载类库推荐?

------解决思路----------------------
android Volley 框架不错,另外强烈建议看 android 关于 bitmap 优化的章节,你会学到很多

地址:
http://developer.android.com/training/displaying-bitmaps/index.html
------解决思路----------------------
Image-Loader,一直在用这个库。
https://github.com/nostra13/Android-Universal-Image-Loader
------解决思路----------------------
以前写过的Android oom 分析 http://blog.csdn.net/vshuang/article/details/39647167
------解决思路----------------------
本人制作的大型安卓游戏,有很多界面是同时显示几十,甚至上百的高清图片,内存都不会溢出。
现在公布显示图片的方法,这个方法消耗内存很小,而且图片不会失真,显示效果很好。
public static BitmapDrawable dr(Context c, int r) {
Bitmap bitmap = readBitMap(c, r);
BitmapDrawable bd = new BitmapDrawable(bitmap);
// bd.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
bd.setDither(true);
return bd;
}

public static Bitmap readBitMap(Context context, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}
调用图片例子this.setBackgroundDrawable(Common.dr(context1, R.drawable.bg_world));
  相关解决方案