1

我不断收到 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

4

2 回答 2

10

您需要将选项传递给解码调用:

BitmapFactory.decodeFile(rawImageName, options);
于 2013-01-15T10:02:03.023 回答
2
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = 4; // 1/4
o2.inPurgeable = true;
Bitmap b=BitmapFactory.decodeFile(imagePath,o2);

试试这个。并在使用后调整图像大小并使位图对象为空。调用System.gc();它不会调用 gc,但它会给出提示。也不要制作很多位图对象。重复使用相同的位图对象并在使用后使其为空。

于 2013-01-15T10:16:47.403 回答