2011-12-13 16 views
9

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!

答えて

13

ビットマップがリサイクルされている可能性があります。

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

ビットマップを長時間ハングアップさせたい場合は、それをコピーする必要があります。

+0

それでした!私はそのラインを取り出し、完璧に働いた!ありがとう!!! – Lucia

+2

あなたはラインを外すべきではありません。キャッシュから提供されるビットマップは、それを所有するビューによっていつでもリサイクルされ得る。代わりに 'Bitmap.copy()'を使って独自のビットマップのコピーを取る必要があります。 –

+0

コピー方法を説明できますか? – Lucia

14

これは私の問題を解決しました。

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. 
関連する問題