android – 无法压缩回收的位图

我试图将一个布局保存在SDCard中的图像中,但是我收到这个错误.我尝试了几个代码,我发现在这个论坛,但所有的都有相同的压缩呼叫,给出的错误.

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

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,v.getMeasuredWidth(),v.getMeasuredHeight());

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

这是将其保存在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());
}

我收到错误

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

有任何想法吗?

解决方法

这可能导致位图被回收:
v.setDrawingCacheEnabled(false); // clear drawing cache

如果您希望位图挂起更长时间,则应将其复制.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...