无法压缩回收的位图 [英] Can't compress a recycled bitmap

查看:77
本文介绍了无法压缩回收的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将版式保存到SDCard中的图像中,但是出现此错误.我尝试了在该论坛中找到的几个代码,但所有代码都具有相同的compress调用,从而导致错误.

I'm trying to save a Layout into an Image in the SDCard but I get this error. I tried several codes I found in this forum but all of them have the same compress call that is giving the error.

这是我用于保存图像的代码:

This is the code I use for saving the image:

private Bitmap TakeImage(View v) {
        Bitmap screen = null;
        try {
            v.setDrawingCacheEnabled(true);

            v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

            v.buildDrawingCache(true);
            screen = v.getDrawingCache();
            v.setDrawingCacheEnabled(false); // clear drawing cache
        } catch (Exception e) {
            e.printStackTrace();
        }
        return screen;
    }

这是将其保存在SDCard中的代码:

And this is the code for saving it in the SDCard:

private void saveGraph(Bitmap graph, Context context) throws IOException {
        OutputStream fOut = null;
        File file = new File(Environment.getExternalStorageDirectory()
                + File.separator + "test.jpg");
        fOut = new FileOutputStream(file);

        graph.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
        fOut.flush();
        fOut.close();

        MediaStore.Images.Media.insertImage(getContentResolver(),
                file.getAbsolutePath(), file.getName(), file.getName());
}

我遇到了错误:

无法在compress调用中压缩回收的位图!

Can't compress a recycled bitmap in the compress call!

推荐答案

这可能导致位图被回收:

This is probably causing the bitmap to be recycled:

v.setDrawingCacheEnabled(false); // clear drawing cache

如果您希望位图保留更长的时间,则应将其复制.

If you want the bitmap to hang around longer, then you should copy it.

这篇关于无法压缩回收的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆