안드로이드 개발 질문/답변
(글 수 45,052)
제가 비트맵을 생성할때 createBitmap()메소드를 사용해서 만들지 않고
MediaStore.Images.Media.getBitmap()메소드를 사용해서 content의 uri 값을 지정해서
Bitmap을 만들고 있습니다.
근데 문제는 이미지의 해상도가 무지 큰 (예를 들어 1024*2048같은...) 이미지의 경우는 비트맵을 생성하다가
MemoryOverflow문제로 죽습니다.
Option값을 주고 싶은데 어떻게 해야 할까요? 이미 만들어진 비트맵에 createBitmap으로 다시 만들어야 할까요?
근데 그러면 메모리 문제가 더 심해질꺼 같긴 하지만..
도움 말씀 좀 부탁드리겠습니다.
비트맵 관련 문제 이해도 힘들고 해결도 힘드내요..
수고하세요
2010.05.03 03:35:03
안드로이드 기본 갤러리에서는 이렇게 사용하네요.. 자세한 것은 갤러리 소스를 참고하심이... Util.java 입니다.
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 == IImage.UNCONSTRAINED) ? 1 :
(int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == IImage.UNCONSTRAINED) ? 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 == IImage.UNCONSTRAINED) &&
(minSideLength == IImage.UNCONSTRAINED)) {
return 1;
} else if (minSideLength == IImage.UNCONSTRAINED) {
return lowerBound;
} else {
return upperBound;
}
}
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 == IImage.UNCONSTRAINED) ? 1 :
(int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == IImage.UNCONSTRAINED) ? 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 == IImage.UNCONSTRAINED) &&
(minSideLength == IImage.UNCONSTRAINED)) {
return 1;
} else if (minSideLength == IImage.UNCONSTRAINED) {
return lowerBound;
} else {
return upperBound;
}
}