【问题标题】:OutOfMemoryError when trying to compress bitmap尝试压缩位图时出现 OutOfMemoryError
【发布时间】:2014-09-07 14:10:23
【问题描述】:

我正在使用android-crop 库。如果用户尝试从大图像(来自手机摄像头的图像)中裁剪,我正在处理令人沮丧的内存不足异常。

我尝试压缩生成的位图,但在尝试压缩位图时仍然得到OutOfMemoryError。这是我的代码:请让我知道我做错了什么和/或如何防止这种内存不足错误?

private void handleCrop(int resultCode, Intent result) {
    if (resultCode == RESULT_OK) {

        if (mBitmap != null) {
            mBitmap.recycle();
            mBitmap = null;
        }

        Bitmap temp; //temporary bitmap

        try {
            temp = MediaStore.Images.Media.getBitmap(getContentResolver()
                    , Crop.getOutput(result)); //load image from URI

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            temp.compress(Bitmap.CompressFormat.JPEG, 60, out);
            mBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

            out.close();
            temp.recycle();
            temp = null;
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "error loading photo. please try with another photo"
                    , Toast.LENGTH_SHORT).show();
            return;
        }

        if (mBitmap != null) {
            mImageViewProfilePhoto.setImageBitmap(mBitmap);
            enableFinalization();
        }

    }
}

感谢 Gabe 的观点,我可以像这样解决问题:

private void handleCrop(int resultCode, Intent result) {
    if (resultCode == RESULT_OK) {

        if (mBitmap != null) {
            mBitmap.recycle();
            mBitmap = null;
        }

        try {
            mBitmap = MediaStore.Images.Media.getBitmap(getContentResolver()
                    , Crop.getOutput(result)); //load image from URI

            File tempImageFile = new File(mContext.getFilesDir().getAbsolutePath()
                    , "temp_1311_14_hahahah_lol_WTF_shit_1414.bin");

            FileOutputStream out = new FileOutputStream (tempImageFile);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 30, out);
            mBitmap.recycle();
            mBitmap = null;

            mBitmap = BitmapFactory.decodeFile(tempImageFile.getPath());
            boolean deleted = tempImageFile.delete();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "error loading photo. please try with another photo"
                    , Toast.LENGTH_SHORT).show();
            return;
        }

        if (mBitmap != null) {
            mImageViewProfilePhoto.setImageBitmap(mBitmap);
            enableFinalization();
        }

    } else if (resultCode == Crop.RESULT_ERROR) {
        Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
        disableFinalization();
    }
}

希望对其他人有用。

【问题讨论】:

    标签: java android bitmap out-of-memory crop


    【解决方案1】:

    假设问题不在您的应用程序的其他地方(并且它总是可能与 OOM 相关)- 如果您有一个大图像,那么您基本上是在制作 3 个副本。第一个是原始的,第二个在您的 ByteArrayOutputStream 中,第三个在您的 decodeStream 中。如果图像很大(如 10MB+),则仅此一项就可能导致问题。我建议使用基于磁盘的输出流,看看是否可以解决问题。此外,您可以将 decodeStream 调用放在 temp.recycle 之后,以确保您一次不会有 2 个完整副本。

    【讨论】:

    • 谢谢,解决了删除临时位图和使用文件流 insted 的问题!
    猜你喜欢
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 2012-03-29
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多