我正在尝试将布局保存到SD卡中的图像中,但我收到此错误。我尝试了几个我在这个论坛中找到的代码,但是所有代码都有相同的压缩调用给出错误。
这是我用来保存图片的代码:
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;
}
这是将其保存在SD卡中的代码:
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());
}
我收到了错误:
无法在压缩调用中压缩回收的位图!
答案 0 :(得分:15)
这可能导致位图被回收:
v.setDrawingCacheEnabled(false); // clear drawing cache
如果你想让位图更长时间地挂起,那么你应该复制它。
答案 1 :(得分:15)
这解决了我的问题。
View drawingView = get_your_view_for_render;
drawingView.buildDrawingCache(true);
Bitmap bitmap = drawingView.getDrawingCache(true).copy(Config.RGB_565, false);
drawingView.destroyDrawingCache();
// bitmap is now OK for you to use without recycling errors.
答案 2 :(得分:0)
解决方案是: 你只需要复制位图。
imageneViewer.setImageBitmap(lienzo.getDrawingCache().copy(Bitmap.Config.RGB_565, false));