6

मैं एक एंड्रॉइड एप्लिकेशन में काम कर रहा हूं और मैं एक छवि दृश्य को छवि को बांधने के लिए बिटमैप का उपयोग कर रहा हूं। मेरी आवश्यकता है कि ImageView को घुमाएं और उस छवि दृश्य को सीमा दें। मैंने इसे सफलतापूर्वक कार्यान्वित किया है, लेकिन एप्लिकेशन के बाद इस गतिविधि का उपयोग करने के बाद दो बार तीन बार "बल बंद" त्रुटि दिखाई देती है जिसमें बिटमैप वीएम मेमोरी से बाहर है। कृपया मेरे कोड में बिटमैप मेमोरी खपत को कम करने में मेरी मदद करें। और मुझे बताएं कि इसके लिए कोड कैसे संशोधित करें?एंड्रॉइड में स्मृति त्रुटि से बिटमैप ...

final int BORDER_WIDTH = 5; 
     // Set the border color 
     final int BORDER_COLOR = Color.WHITE; 
     Bitmap res = Bitmap.createBitmap(CAPTURE_IMAGE.getWidth() + 2 
       * BORDER_WIDTH, CAPTURE_IMAGE.getHeight() + 2 * BORDER_WIDTH, 
       CAPTURE_IMAGE.getConfig()); 
     System.gc(); 
     Canvas canvas = new Canvas(res); 
     Paint paint = new Paint(); 
     paint.setColor(BORDER_COLOR); 
     canvas.drawRect(0, 0, res.getWidth(), res.getHeight(), paint); 

     canvas.drawBitmap(CAPTURE_IMAGE, BORDER_WIDTH, BORDER_WIDTH, paint); 
     Matrix mat = new Matrix(); 
     // Set the Imageview position 
     mat.postRotate(355); 

     bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(), 
       res.getHeight(), mat, true); 
     System.gc(); 
     res.recycle(); 
     res = null; 
     paint = null; 
     canvas = null; 
     mat = null; 
     // Set the captured bitmap image in the imageview 
     mShareImageView.setImageBitmap(bMapRotate); 
+1

मैंने एक अच्छा जेएनआई समाधान बनाया है जो अधिकतम ढेर आकार सीमा बाधा को हटाकर आउट-ऑफ-मेमोरी से बचाता है। [** यहां एक लिंक है **] (http://stackoverflow.com/questions/14398670/android-rotating-a-bitmap-using-jni-ndk#comment20033361_14398670) मेरे स्निपेट में। कुछ नोट्स: - "uint32_t" के साथ "uint16_t" के प्रत्येक उदाहरण को कोड में प्रतिस्थापित करें (यह मेरे कोड पर बग है जिसे मैंने पूछा है)। - इनपुट और आउटपुट बिटमैप 8888 कॉन्फ़िगरेशन (जो एआरजीबी है) के साथ होना चाहिए - प्रक्रिया के दौरान इनपुट बिटमैप का पुनर्नवीनीकरण किया जाएगा। - कोड छवि 90 डिग्री काउंटर घड़ी के अनुसार घुमाता है। बेशक आप इसे –

उत्तर

3

मुझे लगता है कि आप इस

bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(), 
       res.getHeight(), mat, true); 


Bitmap myBitmap = ShrinkBitmap(bMapRotate , 300, 300); 

mShareImageView.setImageBitmap(myBitmap); 


private Bitmap ShrinkBitmap(String file, int width, int height) { 
     // TODO Auto-generated method stub 
     BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
     bmpFactoryOptions.inJustDecodeBounds = true; 
     Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 

     int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height); 
     int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width); 

     if (heightRatio > 1 || widthRatio > 1) 
     { 
     if (heightRatio > widthRatio) 
     { 
      bmpFactoryOptions.inSampleSize = heightRatio; 
     } else { 
      bmpFactoryOptions.inSampleSize = widthRatio; 
     } 
     } 

     bmpFactoryOptions.inJustDecodeBounds = false; 
     bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 
    return bitmap; 
    } 

यह मेरे लिए काम किया है और मैं वी एम स्मृति अपवाद से बाहर

+1

हाय के आधार पर बदल सकते हैं। उत्तर के लिए धन्यवाद। लेकिन विधि में ShrinkBitmap यह एक पैरामीटर फ़ाइल पास करता है जो मेरे पास नहीं है। – Arun

+1

लेकिन आपको कुछ ऐसे फाइलों को चुनना होगा जहां आप –

+1

का उपयोग नहीं कर सकते हैं, यह परिणामस्वरूप छवि आकार बदल देगा ... – Caner

1

बिटमैप बचा अंत करने के लिए gc() कॉल ले जाने का प्रयास की तरह समारोह हटना उपयोग करना चाहिए।

res.recycle(); 
    res = null; 
    paint = null; 
    canvas = null; 
    mat = null; 
    System.gc(); 
3

mainfest फ़ाइल ---> जोड़ने एंड्रॉयड में: = "true largeHeap: यह आप res = null तो सेट करने के बाद कि यह अप्रयुक्त स्मृति जारी कर सकते हैं चलाने चाहिए largeHeap:" सही "

+0

दूसरों के लिए बस एक नोट: मैंने इसे मैनिफेस्ट में अपने '' टैग में डालने का प्रयास किया, जिससे त्रुटियां उत्पन्न हुईं, और यह काम नहीं कर सका। लेकिन जब मैंने इसे '' टैग में रखा, तो यह काम करता था। साथ ही, 'एंड्रॉइड होना चाहिए: bigHeap = "true" '। केवल एक कोलन का उपयोग करें। – Azurespot

0

बस जोड़ने एंड्रॉयड "आपकी मेनिफेस्ट फ़ाइल में

रेफरी: bigHeap = true manifest टैग काम नहीं कर रहा है?

संबंधित मुद्दे