Android使用BitmapFactory.Options解决加载大图片内存溢出问题

来源:互联网 发布:北京淘宝培训小班授课 编辑:程序博客网 时间:2024/06/05 15:41
由于Android对图片使用内存有限制,若是加载几兆的大图片便内存溢出。Bitmap会将图片的所有像素(即长x宽)加载到内存中,
如果图片分辨率过大,会直接导致内存溢出(java.lang.OutOfMemoryError)

,只有在BitmapFactory加载图片时使用BitmapFactory.Options对相关参数进行配置来减少加载的像素。



package com.razar.alarmfinder.utils;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;




public class BitmapUtil {

/*
 * 设置缩放大小对图片作处理
 * Android使用BitmapFactory.Options解决加载大图片内存溢出问题
 * 由于Android对图片使用内存有限制,若是加载几兆的大图片便内存溢出。Bitmap会将图片的所有像素(即长x宽)加载到内存中,
 * 如果图片分辨率过大,会直接导致内存溢出(java.lang.OutOfMemoryError)
 * ,只有在BitmapFactory加载图片时使用BitmapFactory.Options对相关参数进行配置来减少加载的像素。
 */
public static Bitmap getBitmapFromResource(Resources res, int resId,int width, int height) {
         BitmapFactory.Options opts = null;
         if (width > 0 && height > 0) {
             opts = new BitmapFactory.Options();
             opts.inJustDecodeBounds = true;
             BitmapFactory.decodeResource(res, resId, opts);
             // 计算图片缩放比例
             final int minSideLength = Math.min(width, height);
             opts.inSampleSize = computeSampleSize(opts, minSideLength,
                     width * height);
             opts.inJustDecodeBounds = false;
             opts.inInputShareable = true;
             opts.inPurgeable = true;
         }
         try {
             return BitmapFactory.decodeResource(res, resId, opts);
         } catch (OutOfMemoryError e) {
             e.printStackTrace();
         }
         return null;
 }



public static int computeSampleSize(BitmapFactory.Options options,
         int minSideLength, int maxNumOfPixels) {
     int initialSize = computeInitialSampleSize(options, minSideLength,
             maxNumOfPixels);
 
    int roundedSize;
     if (initialSize <= 8) {
         roundedSize = 1;
         while (roundedSize < initialSize) {
             roundedSize <<= 1;
         }
     } else {
         roundedSize = (initialSize + 7) / 8 * 8;
     }
 
    return roundedSize;
 }
 
private static int computeInitialSampleSize(BitmapFactory.Options options,
         int minSideLength, int maxNumOfPixels) {
     double w = options.outWidth;
     double h = options.outHeight;
 
    int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
             .sqrt(w * h / maxNumOfPixels));
     int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math
             .floor(w / minSideLength), Math.floor(h / minSideLength));
 
    if (upperBound < lowerBound) {
         // return the larger one when there is no overlapping zone.
         return lowerBound;
     }
 
    if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
         return 1;
     } else if (minSideLength == -1) {
         return lowerBound;
     } else {
         return upperBound;
     }
 }

}



//调用

private void setBackground() {
// 读取图片
// Bitmap welcomeBit = getRes("welcome");


// 获取分辨率
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);


int nowWidth = dm.widthPixels; // 当前分辨率 宽度
int nowHeigth = dm.heightPixels; // 当前分辨率高度

nowBitMap = BitmapUtil.getBitmapFromResource(getResources(), R.drawable.welcome, nowWidth, nowHeigth); 
mTvWelcomePage.setImageBitmap(nowBitMap);
}



//在适当的时候及时回收图片占用的内存  通常Activity或者Fragment在onStop/onDestroy时候就可以释放图片资源:  

@Override
protected void onDestroy() {
//如果能够获得Bitmap对象的引用,就需要及时的调用Bitmap的recycle()方法来释放Bitmap占用的内存空间,而不要等Android系统来进行释放。
if(nowBitMap != null && !nowBitMap.isRecycled()){ //先判断是否已经回收
//回收并且置为null
nowBitMap.recycle();
nowBitMap = null;
Log.d(TAG, "退出activity时回收bitmap...");
}
System.gc(); //告知系统回收bitmap垃圾以免占用系统内存
super.onDestroy();
}

0 0
原创粉丝点击