Android ImageLoader must be init with configuration before using 错误解决方法
2016-09-20 17:43
639 查看
开发过程中用到了开源项目Android-Universal-Image-Loader,Universal-Image-Loader,一个强大的图片加载框架,具有以下的特性:
字面意思是在使用前要初始化
要加一句话:
ps: 官方demo中的初始化方法是在application类中调用的,所以,可以自定义MyApplication
新建一个MyApplication继承Application,Application是单例 (singleton)模式的一个类。且application对象的生命周期是整个程序中最长的,它的生命周期就等于这个程序的生命周期。因为它是全局的单例的,所以在不同的Activity,Service中获得的对象都是同一个对象。所以通过Application来进行一些数据传递,数据共享等数据缓存等操作。在这里我们来创建图片加载器ImageLoader的配置参数。
ImageLoaderConfiguration使用了建造者模式配置参数,设置了线程池中线程个数,内存存储大小,数量,硬盘存储大小,数量等参数。
最后调用ImageLoader.getInstance().init(config)将设置参数传递进去,这里用的是单例模式–懒汉式双重校验锁 。
使用的时候
// 通过ImageLoader来获取网络图片
DisplayImageOptions用来配置图片显示的选项,比如图片在加载中ImageView显示的图片,加载失败显示的图片,是否需要使用内存缓存,是否需要使用文件缓存等等。这里都设置true,就不用每次都从网络上加载图片。
Universal-Image-Loader提供了PauseOnScrollListener这个类来控制ListView,GridView滑动过程中停止去加载图片。
第一个参数就是我们的图片加载对象ImageLoader, 第二个是控制是否在滑动过程中暂停加载图片,如果需要暂停传true就行了,第三个参数控制猛的滑动界面的时候图片是否加载。
1、多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等
2、支持随意的配置ImageLoader,例如线程池,图片下载器,内存缓存策略,硬盘缓存策略,图片显示选项以及其他的一些配置
3、支持图片的内存缓存,文件系统缓存或者SD卡缓存
4、支持图片下载过程的监听
5、根据控件(ImageView)的大小对Bitmap进行裁剪,减少Bitmap占用过多的内存
6、较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片,一般使用在ListView,GridView中,滑动过程中暂停加载图片,停止滑动的时候去加载图片
7、提供在较慢的网络下对图片进行加载
Android-Universal-Image-Loader 在使用的过程中碰到了一个问题。程序运行不起来,报以下错误:java.lang.RuntimeException: ImageLoader must be init with configuration before using
字面意思是在使用前要初始化
要加一句话:
imageLoader.init(ImageLoaderConfiguration.createDefault(MainActivity.this));
ps: 官方demo中的初始化方法是在application类中调用的,所以,可以自定义MyApplication
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); } }
新建一个MyApplication继承Application,Application是单例 (singleton)模式的一个类。且application对象的生命周期是整个程序中最长的,它的生命周期就等于这个程序的生命周期。因为它是全局的单例的,所以在不同的Activity,Service中获得的对象都是同一个对象。所以通过Application来进行一些数据传递,数据共享等数据缓存等操作。在这里我们来创建图片加载器ImageLoader的配置参数。
ImageLoaderConfiguration使用了建造者模式配置参数,设置了线程池中线程个数,内存存储大小,数量,硬盘存储大小,数量等参数。
最后调用ImageLoader.getInstance().init(config)将设置参数传递进去,这里用的是单例模式–懒汉式双重校验锁 。
使用的时候
// 通过ImageLoader来获取网络图片
ImageLoader.getInstance().displayImage(imageUrls.get(position),imageView);
DisplayImageOptions用来配置图片显示的选项,比如图片在加载中ImageView显示的图片,加载失败显示的图片,是否需要使用内存缓存,是否需要使用文件缓存等等。这里都设置true,就不用每次都从网络上加载图片。
Universal-Image-Loader提供了PauseOnScrollListener这个类来控制ListView,GridView滑动过程中停止去加载图片。
listview.setOnScrollListener(new PauseOnScrollListener(ImageLoader .getInstance(), true, false));
第一个参数就是我们的图片加载对象ImageLoader, 第二个是控制是否在滑动过程中暂停加载图片,如果需要暂停传true就行了,第三个参数控制猛的滑动界面的时候图片是否加载。
相关文章推荐
- Android ImageLoader must be init with configuration before using 错误解决方法
- ImageLoader must be init with configuration before using 错误解决方法
- ImageLoader must be init with configuration before using 错误解决方法
- ImageLoader must be init with configuration before using 错误解决方法
- ImageLoader must be init with configuration before using 错误解决方法
- ImageLoader must be init with configuration before using 错误解决方法
- ImageLoader must be init with configuration before using 错误解决方法
- ImageLoader must be init with configuration before using 错误解决方法
- java.lang.RuntimeException: ImageLoader must be init with configuration before using的解决方法
- androidの异常:java.lang.IllegalStateException: ImageLoader must be init with configuration befor 错误解决方法
- Android: ImageLoader must be init with configuration before using
- 解决ImageLoader must be init with configuration before using
- 解决使用imageload遇到ImageLoader must be init with configuration before using这种异常
- ImageLoader must be init with configuration before using
- ImageLoader must be init with configuration before using...
- ImageLoader must be init with configuration before using
- ImageLoader must be init with configuration before using
- ImageLoader must be init with configuration before using
- ImageLoader must be init with configuration before using
- 图片异步加载java.lang.IllegalStateException: ImageLoader must be init with cconfiguration before using问题