【发布时间】:2012-12-29 09:31:09
【问题描述】:
在我的 Android 应用程序中尝试解码来自相机的图像时,我不断收到 OutOfMemory 异常。有很多问题可以解决这个问题,但我的情况特别奇怪,因为即使只是尝试使用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 解码时可能导致异常的原因是什么?
【问题讨论】:
-
向我们展示您的堆栈跟踪。
-
我认为你应该看看这个链接:developer.android.com/training/displaying-bitmaps/…
-
好吧,这恰好是一个愚蠢的复制粘贴错误。应该删除问题吗?对别人有什么价值吗?
标签: java android image android-camera out-of-memory