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, 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());
}
私はエラーを取得しています:
Can't compress a recycled bitmap in the compress call!
それでした!私はそのラインを取り出し、完璧に働いた!ありがとう!!! – Lucia
あなたはラインを外すべきではありません。キャッシュから提供されるビットマップは、それを所有するビューによっていつでもリサイクルされ得る。代わりに 'Bitmap.copy()'を使って独自のビットマップのコピーを取る必要があります。 –
コピー方法を説明できますか? – Lucia