【问题标题】:Image Processing: "dalvikvm: external allocation too large for this process" error图像处理:“dalvikvm:此进程的外部分配太大”错误
【发布时间】:2012-08-04 18:05:07
【问题描述】:

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

这是 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

更新::

    @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);回收();但它不起作用

【问题讨论】:

  • 你能发布堆栈跟踪吗?

标签: android memory bitmap


【解决方案1】:

这是一个内存泄漏错误。

您的应用被分配了一个大小约为 24MB 的堆。但是您尝试编辑的图像大于堆大小。

发生这种情况是因为您没有释放内存。 Android 的 Dalvik VM 不处理 GC-ing 应用程序使用的本机内存。所以,在Android中处理图片时,需要显式使用recycle()。这释放了本机内存。

更多详情:Changing ImageView content causes OutOfMemoryError

【讨论】:

  • 您需要回收Bitmap图像对象。所以,在 imageView.setBitmap() 之后,调用 bMap.recycle()。更多详情:stackoverflow.com/questions/3823799/…
  • 当我在 imageBiew.setBitmap 之后调用回收时它强制关闭
  • 如果您只执行 System.GC() 会发生什么。现在评论 recycle()。我希望我已经帮助您查明了问题的原因。您可能需要在网上进行一些挖掘以了解如何回收/GC 图像。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2012-04-17
    • 1970-01-01
    • 2011-09-13
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 2011-10-04
    相关资源
    最近更新 更多