使用inJustDecodeBounds = true的OutOfMemory解码图像

Fir*_*his 1 java android image out-of-memory android-camera

我不断尝试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);
}
Run Code Online (Sandbox Code Playgroud)

这是触发异常的代码:

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
            .
            .
            .
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用非常高的采样率解码图像,但我仍然得到相同的异常:

options.inSampleSize = 20;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap photo = BitmapFactory.decodeFile(rawImageName); // Again the exception
Run Code Online (Sandbox Code Playgroud)

除此之外,应用程序似乎运行正常,并有足够的可用内存.我可以在图库应用中正确打开图像.将图像移动到另一个目录没有帮助.什么可能导致它的想法?解码时可能导致异常的原因是什么inJustDecodeBounds = true

Hen*_*nry 10

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

BitmapFactory.decodeFile(rawImageName, options);
Run Code Online (Sandbox Code Playgroud)