나는 많은 이미지 (비트 맵)를 재생하고있는 하나의 데모 애플리케이션을 개발 중이다. 둘 이상의 활동에 동일한 이미지를 표시하기 위해 전역 비트 맵 객체를 사용하고 있습니다. 그러나 Bitmap.createBitmap (...)을 사용하여 비트 맵을 만들려고 할 때 OutOfMemoryError를 사용하고 있습니다. 나는 this code을 사용해 보았지만 여전히 같은 오류가 발생하고 있으며 내 응용 프로그램을 충돌시키고 OutOfMemoryError를 통해 충돌합니다.안드로이드의 비트 맵 OutOfMemoryError
나는 이것에 붙어있다. 누구든지 이것을 피할 수있는 해결책이 있습니까?
미리 감사드립니다.
비트 맵 크기를 디코딩하기 위해 아래 코드를 사용하도록 이미지 자르기 후 비트 맵을 가져오고 있습니다.
public static Bitmap loadFromBitmap(Context context, Bitmap b, int maxW, int maxH) throws IOException {
final BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = null;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inJustDecodeBounds = true;
BufferedInputStream stream = null;
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
b.compress(CompressFormat.JPEG, 100, outstream);
InputStream is = new java.io.ByteArrayInputStream(outstream.toByteArray());
stream = new BufferedInputStream(is, 16384);
if (stream != null) {
options.inSampleSize = calculateInSampleSize(options, maxW, maxH);
stream = null;
stream = new BufferedInputStream(is, 16384);
} else {
return null;
}
options.inDither = false;
options.inJustDecodeBounds = false;
options.inPurgeable = true;
bitmap = BitmapFactory.decodeStream(stream, null, options);
if (bitmap != null) bitmap = BitmapUtils.resizeBitmap(bitmap, maxW, maxH);
return bitmap;
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height/(float) reqHeight);
final int widthRatio = Math.round((float) width/(float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
오류 : 사실 options.inPurgeable = 및 op.inInputShareable = 사실 및 코드에서 방법을 절약 일부 메모리를 않습니다와
02-21 15:32:31.160: E/AndroidRuntime(2951): FATAL EXCEPTION: main
02-21 15:32:31.160: E/AndroidRuntime(2951): java.lang.OutOfMemoryError
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:374)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:404)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.Activity.performCreate(Activity.java:4465)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.os.Looper.loop(Looper.java:137)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.ActivityThread.main(ActivityThread.java:4447)
02-21 15:32:31.160: E/AndroidRuntime(2951): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 15:32:31.160: E/AndroidRuntime(2951): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 15:32:31.160: E/AndroidRuntime(2951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-21 15:32:31.160: E/AndroidRuntime(2951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-21 15:32:31.160: E/AndroidRuntime(2951): at dalvik.system.NativeStart.main(Native Method)
이 [링크] (http://developer.android.com/training /displaying-bitmaps/load-bitmap.html#read-bitmap) 도움이 될 것입니다 –
감사합니다 램,하지만 내 질문에 동일한 링크를 언급했다. : – anddev
@anddev 결국 당신은 이미지를 늘릴 것인가? 어디에서 이미지를 얻고 있습니까? – Prateek