कृपया मेरे संबंधित सवाल पर एक नजर है: को छोटे से छोटा पृष्ठभूमि छवि रख कर अपने आवेदन की स्मृति के उपयोग को कम करने के
High resolution Image - OutOfMemoryError
प्रयास करें।
सुनिश्चित करें कि आप एक पृष्ठभूमि के रूप में सेट छवियों सही ढंग से लोड कर रहे हैं उपयोग करने के बाद अशक्त करने के लिए संदर्भ सेट (जैसे आकार में फसल, उदाहरण के लिए स्क्रीन आकार फिट) और स्मृति की आवश्यकता के साथ ही उन्हें अब आवश्यक नहीं है।
सुनिश्चित करें कि आपके पास स्मृति में आपके बिटमैप का केवल एक उदाहरण है। इसे प्रदर्शित करने के बाद, recycle()
पर कॉल करें और अपना संदर्भ शून्य पर सेट करें। कोड के इस खूबसूरत टुकड़े के लिए Adam Stelmaszczyk को
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height/(float) reqHeight);
final int widthRatio = Math.round((float) width/(float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
धन्यवाद:
इस तरह आप अपने छवियों को लोड कर सकता है।
स्रोत
2013-08-23 22:11:04
जैसा कि इन सवालों में से कई में उल्लेख किया गया है, विशाल छवियों का उपयोग न करें (> अपने मामले में 30 एमबी) पृष्ठभूमि छवियों का उपयोग न करें। – kabuko
स्टैक ओवरफ्लो में इस तरह के कई समान प्रश्न हैं। मैंने इसे हल करने के कई विकल्पों का सारांश बनाने की कोशिश की है: http://stackoverflow.com/questions/11820266/android-bitmapfactory-decodestream-out-of-memory-with-a-400kb-file-with-2mb- एफ/16528487 # 16528487 –
एक बहुत बड़ी छवि का उपयोग न करने के अलावा, उपकरणों की क्षमता पर विचार करना चाहिए, असली समस्या यह है कि डिवाइस में पृष्ठभूमि की तरह चार्ज करने की क्षमता नहीं है। –