我不断收到 OutOfMemory 异常,试图在我的 Android 应用程序中解码来自相机的图像。有很多问题可以解决这个问题,但我的情况特别奇怪,因为即使只是试图获得options.inJustDecodeBounds=true
.
这是启动相机的代码:
protected void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(IMAGE_PATH, "camera.jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
startActivityForResult(takePictureIntent, 0);
}
下面是触发异常的代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK ) {
String rawImageName = new String(IMAGE_PATH + "/camera.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(rawImageName); // The exception is thrown here
.
.
.
}
}
我尝试使用非常高的采样率对图像进行解码,但仍然遇到相同的异常:
options.inSampleSize = 20;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap photo = BitmapFactory.decodeFile(rawImageName); // Again the exception
除此之外,应用程序似乎运行正常并且有足够的可用内存。我可以在图库应用程序中正确打开图像。将图像移动到其他目录没有帮助。任何想法可能导致它?使用 解码时可能导致异常的原因是什么inJustDecodeBounds = true
?