2011-12-13 23 views
9

Bir Düzen'i SDCard'ta bir Görüntü içine kaydetmeye çalışıyorum ancak bu hatayı alıyorum. Bu forumda bulduğum birkaç kodu denedim ama hepsinin hata veren aynı sıkıştırma çağrısı var. Geri dönüştürülmüş bir bitmapi sıkıştırılamıyor

Bu

ben görüntüyü kaydetmek için kullanmak kodudur:

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; 
    } 

Ve bu sdcard kaydetmek üzere kodudur:

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()); 
} 

Şu hatayı alıyorum:

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

cevap

13

Bu muhtemelen bitmap geri dönüşümlü olması neden olan:

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

Bitmap'in daha uzun süre takılmasını istiyorsanız, kopyalamalısınız.

+0

Bu çizgiyi çıkardım ve mükemmel bir şekilde çalıştım! Teşekkürler!!! – Lucia

+2

Çizgiyi çıkarmamalısınız; Önbellekten sağlanan bitmap, sahibi olan görünüm tarafından herhangi bir zamanda geri dönüştürülebilir. Bunun yerine Bitmap'in kendi kopyasını Bitmap.copy() 'yi kullanarak almanız gerekiyor. –

+0

Nasıl kopyalanacağını açıklayabilir misiniz? – Lucia

14

Bu, sorunlarımı çözdü.

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. 
İlgili konular