开源框架com.nostra13.universalimageloader加载图片时抛出OUTOFMEMORY错误的处理方法

     最近在开发应用的一个游戏模块中需缓存大量的网络图片到本地,为图方便直接使用的com.nostra13.universalimageloader提供的图片缓存方法,可在部分内存容量较小手机运行时ImageLoader.loadImage()偶尔抛出OutOfMemoryerror异常进而导致程序崩溃退出。针对该问题我们可以从如下两方法来解决:

    1、尽量减少ImageLoader占用的内存避免出现内存溢出的问题

           对DisplayImageOptions类使用如下的配置

   new DisplayImageOptions.Builder()
            .cacheInMemory(false)   //设置图片不缓存于内存中
            .cacheOnDisc(true)
            .bitmapConfig(Bitmap.Config.RGB_565)    //设置图片的质量
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)    //设置图片的缩放类型,该方法可以有效减少内存的占用
            .build();
    
    2、对错误进行处理

          对图片加载方法添加监听事件,对出现的异常进行特定的处理:

imageLoader.loadImage(uri, options, new ImageLoadingListener() {
            
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                // TODO Auto-generated method stub
                switch (failReason.getType()) {
                case IO_ERROR:
                    //handler.sendEmptyMessage();
                    break;
                case DECODING_ERROR:
                    break;
                    
                case NETWORK_DENIED:
                    break;
                    
                case OUT_OF_MEMORY:
                    break;
                    
                case UNKNOWN:
                    break;
                default:
                    break;
                }
            }
            
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                // TODO Auto-generated method stub
                
            }
        });
        FailReason为该库中的异常类该类封装了枚举变量FailType,该类的定义如下:

public class FailReason {

	private final FailType type;

	private final Throwable cause;

	public FailReason(FailType type, Throwable cause) {
		this.type = type;
		this.cause = cause;
	}

	/** @return {@linkplain FailType Fail type} */
	public FailType getType() {
		return type;
	}

	/** @return Thrown exception/error, can be <b>null</b> */
	public Throwable getCause() {
		return cause;
	}

	/** Presents type of fail while image loading */
	public static enum FailType {
		/** Input/output error. Can be caused by network communication fail or error while caching image on file system. */
		IO_ERROR,
		/**
		 * Error while
		 * {@linkplain android.graphics.BitmapFactory#decodeStream(java.io.InputStream, android.graphics.Rect, android.graphics.BitmapFactory.Options)
		 * decode image to Bitmap}
		 */
		DECODING_ERROR,
		/**
		 * {@linkplain com.nostra13.universalimageloader.core.ImageLoader#denyNetworkDownloads(boolean) Network
		 * downloads are denied} and requested image wasn't cached in disc cache before.
		 */
		NETWORK_DENIED,
		/** Not enough memory to create needed Bitmap for image */
		OUT_OF_MEMORY,
		/** Unknown error was occurred while loading image */
		UNKNOWN
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值