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;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Shirates에서 상대 선택기를 사용하는 방법 - 1부 -이것은 간단하고 강력한 표현입니다(참조). 오른쪽, 아래, 왼쪽, 위 방향으로 상대적으로 위젯을 얻을 수 있습니다. 올바른 방향의 위젯 올바른 방향으로 입력 라벨을 올바른 방향으로 올바른 방향으로 이미지 :오른쪽 버...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.