图像处理:“dalvikvm:外部分配对于此过程来说太大”错误

时间:2012-08-04 18:05:07

标签: android memory bitmap

我有一个照片编辑应用。当我导入图像时,它工作正常但是,打开图像进行编辑会导致图像崩溃。

这是logcat输出:


08-04 20:56:16.973: E/dalvikvm-heap(336): 810000-byte external allocation too large for this process.
08-04 20:56:17.073: I/dalvikvm-heap(336): Clamp target GC heap from 25.289MB to 24.000MB
08-04 20:56:17.073: E/GraphicsJNI(336): VM won't let us allocate 810000 bytes

UPDATE ::

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.ivPhoto);
        chosenBitmap = superDecodeFile(new File(picturePath));
        imageView.setBackgroundColor(0);
        Bitmap bMap =   Bitmap.createScaledBitmap(chosenBitmap, 500, 500, true);
        imageView.setImageBitmap(bMap);
    }

}

这是我的onResult代码..所以choosenBitmap显示在imageView(ivPhoto)上 我需要使用.recycle(); 所以我通过回收已经imageView来释放一些内存.. 我在哪里使用.recycle(); ? 我试图改变.setBackgroundColor(0);回收再利用();但它不起作用

2 个答案:

答案 0 :(得分:3)

这是内存泄漏错误。

您的应用分配了大小~24MB的堆。但是您尝试编辑的图像大于堆大小。

这种情况正在发生,因为你没有释放内存。 Android的Dalvik VM不会处理GC应用程序使用的本机内存。因此,在Android中处理图像时,您需要明确使用recycle()。这释放了本机内存。

更多细节:Changing ImageView content causes OutOfMemoryError

答案 1 :(得分:0)

您的图片太大而无法加载到内存中。遗憾的是,您无法更改应用程序使用的最大内存量。有关加载和显示位图的详细信息,请参阅Displaying Bitmaps