Android 는 BitmapFactory. Options 를 사용 하여 큰 그림 메모리 넘 침 문 제 를 해결 합 니 다.

Android 는 BitmapFactory. Options 를 사용 하여 큰 그림 메모리 넘 침 문 제 를 해결 합 니 다.
http://orgcent.com/android-outofmemoryerror-load-big-image/
안 드 로 이 드 는 그림 의 메모리 사용 에 제한 이 있 기 때문에 몇 조 의 큰 그림 을 불 러 오 면 메모리 가 넘 칩 니 다.비트 맵 은 그림 의 모든 픽 셀 (즉, 길이 x 너비) 을 메모리 에 불 러 옵 니 다. 그림 해상도 가 너무 크 면 메모리 넘 침 (java. lang. OutOf Memory Error) 을 초래 합 니 다. 비트 맵 팩 토리 에서 만 그림 을 불 러 올 때 비트 맵 팩 토리. Options 를 사용 하여 불 러 오 는 픽 셀 을 줄 일 수 있 습 니 다.
1. 크기 조정 을 설정 하여 그림 을 처리 합 니 다.
public Bitmap getBitmapFromFile(File dst, int width, int height) {
     if (null != dst && dst.exists()) {
         BitmapFactory.Options opts = null;
         if (width > 0 && height > 0) {
             opts = new BitmapFactory.Options();
             opts.inJustDecodeBounds = true;
             BitmapFactory.decodeFile(dst.getPath(), 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.decodeFile(dst.getPath(), 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;
     }
 }

좋은 웹페이지 즐겨찾기