ImageLoader 加載圖檔及注意事項
作為android加載圖檔的開源架構,com.nostra13.universalimageloader.core.ImageLoader給我們提供了很大的友善,但是在使用過程會産生一些問題,下面是我使用過程及其遇到的一些問題。
一、圖檔加載
1、ImageLoader.getInstance().displayImage(url, imageView);
參數
url:可以為網絡圖檔、本地圖檔圖檔的路徑,也可以是項目中檔案路徑,還可以是内容提提供者的路徑;
1)網絡圖檔
url = "http://image.zidingyi.com/search/1.png"; 即網絡位址。
2)本地圖檔
url = "file:///mnt/sdcard/image.png";
3)項目中assets檔案下圖檔
uri = "assets://image1.png";
4)項目中drawable檔案下圖檔
uri = "drawable://" + R.drawable.ic_launcher;
5)内容提提供者中的圖檔
uri = "content://media/external/audio/albumart/1";
imageView:自己定義的ImageView對象;
從上面可以看出擷取圖檔非常友善,而且都是異步加載。
2、我們不可以增加監聽來判斷圖檔的加載情況
ImageLoader.getInstance().displayImage(url, this, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
//圖檔形如加載,可以做一些提示操作
}
@Override
public void onLoadingFailed(String imageUri, View view,FailReason failReason) {
//圖檔加載失敗
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//圖檔加載成功,可以再次根據需求對圖檔進行處理
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
//圖檔加載取消
}
});
}
3、 還可添加參數DisplayImageOptions
ImageLoader.getInstance().displayImage(url, this, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
//圖檔形如加載,可以做一些提示操作
}
@Override
public void onLoadingFailed(String imageUri, View view,FailReason failReason) {
//圖檔加載失敗
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//圖檔加載成功,可以再次根據需求對圖檔進行處理
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
//圖檔加載取消
}
},displayImageOptions);
}
參數
displayImageOptions:可以通過以下方式設定
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.build();
二、防止OOM産生
産生的oom:
OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available
Failed to allocate a 32 byte allocation with 0 free bytes and 3GB until OOM
java.lang.OutOfMemoryError: Failed to allocate a 32 byte allocation with 0 free bytes and 3GB until OOM
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:634)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:610)
at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:83)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:265)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:223)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:136)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
解決方法:
1、設定DisplayImageOptions 中.cacheInMemory(false) ,讓圖檔不緩存于記憶體中;
2、在DisplayImageOptions的.bitmapConfig中配置使用Bitmap.Config.RGB555
代替Bitmap.Config.ARGB8888,減少記憶體占用;
3、在ImageLoaderConfiguration的.memoryCache()中配置圖檔緩存為軟緩存或弱緩存;
4、在DisplayImageOption中配置imageScaleType(ImageScaleType.IN_SAMPLE_INT);
5、在ImageLoaderConfiguration的.threadPoolSize中減少線程池中線程的個數,最好在5個以下。