1. 程式人生 > >ImageLoader must be init with configuration before using

ImageLoader must be init with configuration before using

als ive 最長 xtra roi 項目 multiple private ble

遇到上面的問題是沒有全局初使化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、提供在較慢的網絡下對圖片進行加載

ImageLoader must be init with configuration before using