2011-12-13 7 views
9

मैं एसडीकार्ड में एक लेआउट को एक छवि में सहेजने की कोशिश कर रहा हूं लेकिन मुझे यह त्रुटि मिलती है। मैंने इस मंच में कई कोडों की कोशिश की लेकिन उनमें से सभी को एक ही संपीड़न कॉल है जो त्रुटि दे रहा है।एक पुनर्नवीनीकरण बिटमैप को संकुचित नहीं कर सकता

इस कोड मैं छवि को बचाने के लिए उपयोग करते हैं:

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

और यह 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!

उत्तर

13

शायद यह बिटमैप को पुनर्नवीनीकरण का कारण बन रहा है:

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

यदि आप बिटमैप को लंबे समय तक लटकना चाहते हैं, तो आपको इसकी प्रतिलिपि बनाना चाहिए।

+0

यह वह था! मैंने उस रेखा को बाहर निकाला और पूरी तरह से काम किया! धन्यवाद!!! – Lucia

+2

आपको लाइन नहीं लेनी चाहिए; कैश से प्रदान किए गए बिटमैप को किसी भी समय उस दृश्य के द्वारा पुनर्नवीनीकरण किया जा सकता है, जिसका मालिक है। आपको वास्तव में 'बिटमैप.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. 
संबंधित मुद्दे