ImageLoader must be init with configuration before using
遇到上面的问题是没有全局初使化ImageLoader,我是在Application中配置了ImageLoaderConfiguration 解决的,当然还有官方的写法
public class MyApplication extends Application {
private MyApplication instance;
@Override
public void onCreate() {
super.onCreate();
instance = this;
initImageloader();
}
public void initImageloader() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_picture_loading)
.showImageOnFail(R.drawable.ic_picture_loadfailed)
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(0).cacheInMemory(true) // default
.cacheOnDisk(true) // default
.considerExifParams(true) // default
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
.bitmapConfig(Bitmap.Config.ARGB_8888) // default
.displayer(new SimpleBitmapDisplayer()) // default
.handler(new Handler()) // default
.build();
File picPath = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "MyApp"+ File.separator + "files");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).memoryCacheExtraOptions(480, 800) //我是用的这种写法
// default = device screen dimensions
.diskCacheExtraOptions(480, 800, null).threadPoolSize(3)
// default
.threadPriority(Thread.NORM_PRIORITY - 1)
// default
.tasksProcessingOrder(QueueProcessingType.FIFO)
// default
.denyCacheImageMultipleSizesInMemory().memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024).memoryCacheSizePercentage(13)
// default
.diskCache(new UnlimitedDiskCache(picPath))
// default
.diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(1000)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
// default
.imageDownloader(new BaseImageDownloader(getApplicationContext())) // default
.imageDecoder(new BaseImageDecoder(true)) // default
.defaultDisplayImageOptions(options) // default
.writeDebugLogs().build();
ImageLoader.getInstance().init(config);
}
}
因为Application是应用生命周期最长的,它是单例的,用来交互Activity和Service的数据,ImageLoaderConfiguration使用了建造者模式配置参数,设置了线程池中线程个数,内存存储大小,数量,硬盘存储大小,数量等参数。
开发过程中用到了开源项目Android-Universal-Image-Loader,Universal-Image-Loader,一个强大的图片加载框架,具有以下的特性:
1、多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等
2、支持随意的配置ImageLoader,例如线程池,图片下载器,内存缓存策略,硬盘缓存策略,图片显示选项以及其他的一些配置
3、支持图片的内存缓存,文件系统缓存或者SD卡缓存
4、支持图片下载过程的监听
5、根据控件(ImageView)的大小对Bitmap进行裁剪,减少Bitmap占用过多的内存
6、较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片,一般使用在ListView,GridView中,滑动过程中暂停加载图片,停止滑动的时候去加载图片
7、提供在较慢的网络下对图片进行加载
最新文章
- javascript作用域链与原型链有联系吗?
- Sublime Text
- Node 连接Mysql并进行增删改查
- webapp新体验Rem实现移动端网页适配详解资源
- 相似元素存在的意义---HTML&;CSS
- POJ 1005 解题报告
- TIME-WAIT和CLOSE-WAIT
- 【转】在ubuntu12.04中安装wine和sourceinsight
- C++中的引用到底是什么
- 用C++进行简单的文件I/O操作-转自VC知识库
- mysql添加远程用户
- Java 4
- WNDCLASS 窗口类结构
- (转)使用JMeter进行Web压力测试
- ztree模糊筛选展开选中节点
- SQL Server 日常维护经典应用
- Netty 中ChannelOption的含义以及使用的场景
- std::nothrow 的使用心得
- JavaScript之事件绑定多个序列执行方法
- hdu3861 强连通分量缩点+二分图最最小路径覆盖
热门文章
- Python OS 文件
- python笔记-print输出
- SQL 关于apply的两种形式cross apply 和 outer apply, with cube 、with rollup 和 grouping
- JEECG与帆软报表集成
- LDA与QDA
- C++不让程序一闪而过
- X86平台简称
- 查询ip地址归属地
- json对象与json字符串互转方法
- 线程相关函数(5)-pthread_rwlock_rdlock(),pthread_rwlock_wrlock() 读写锁