您的位置:首页 > 其它

ImageLoader must be init with configuration before using

2017-06-29 14:15 393 查看

遇到上面的问题是没有全局初使化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、提供在较慢的网络下对图片进行加载

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐