我试图将一个布局保存在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
如果您希望位图挂起更长时间,则应将其复制.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。