2013-10-30 5 views
15

मुझे किसी फ़ाइल से किसी सर्वर पर एक छवि भेजने की आवश्यकता है। सर्वर 2400x2400 के संकल्प में छवि का अनुरोध करता है।बिटमैपफैक्टरी.decode छवियों के साथ स्मृति से बाहर 2400x2400

मुझे क्या करना कोशिश कर रहा हूँ है:

1) सही inSampleSize का उपयोग कर BitmapFactory.decodeFile का उपयोग कर एक बिटमैप प्राप्त करें।

2) 40%

3) के एक गुणवत्ता के साथ जेपीईजी में छवि संक्षिप्त बेस 64

4) सर्वर

भेजा गया में छवि एनकोड, यह मैं पहला कदम हासिल नहीं कर सकते स्मृति अपवाद से बाहर फेंकता है। मुझे यकीन है कि inSampleSize सही है, लेकिन मुझे लगता है कि सैंपलसाइज़ के साथ भी बिटमैप विशाल है (डीडीएमएस में लगभग 30 एमबी)।

कोई विचार यह कैसे कर सकता है? क्या मैं बिटमैप ऑब्जेक्ट के बिना इन चरणों को कर सकता हूं? मेरा मतलब राम स्मृति की बजाय फाइल सिस्टम पर कर रहा है।

// The following function calculate the correct inSampleSize 
Bitmap image = Util.decodeSampledBitmapFromFile(imagePath, width,height); 
// compressing the image 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
image.compress(Bitmap.CompressFormat.JPEG, 40, baos); 
// encode image 
String encodedImage = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT)); 


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

public static Bitmap decodeSampledBitmapFromFile(String path,int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(path,options); 
} 
+0

यू के माध्यम से कर संरक्षित करने के लिए कुछ विचार मिलेगा http://developer.android.com/training/displaying-bitmaps/manage-memory.html –

+0

getBitmap()। रीसायकल(); –

+0

आपके पास एक फ़ाइल में छवि है, है ना? बस इस फाइल को सर्वर पर भेजें और वहां आवश्यक परिवर्तन करें। – Henry

उत्तर

46

आप ARGB_8888 को छोड़ सकते हैं, और बदले RGB_565 का उपयोग करें, और उसके बाद तो छवियों तड़पना अच्छी गुणवत्ता

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = false; 
options.inPreferredConfig = Config.RGB_565; 
options.inDither = true; 
+0

यह बिटमैप 13 एमबी के आकार को कम करता है और यह काम कर रहा है। आपको बहुत - बहुत धन्यवाद! – alasarr

+0

बढ़िया! यह काम करता हैं! धन्यवाद! – Ignacio

1

आप सत्य पर inJustDecodeBounds सेट के साथ BitmapFactory.Options उपयोग करने के लिए:

यह वर्तमान कोड है। इस तरह आप बिटमैप के बारे में जानकारी लोड कर सकते हैं और इसे डाउनसमैप्लिंग के लिए मान की गणना कर सकते हैं (inSampleSize उदाहरण के लिए)

+0

से कम मैंने पहले ही इसे किया है और यह काम नहीं करता है। – alasarr

+0

मैं इसे देखना चाहूंगा। क्या आप कोड को स्निपेट के साथ पोस्ट अपडेट कर सकते हैं? – Blackbelt

+0

मैंने अभी पोस्ट अपडेट किया है। धन्यवाद! – alasarr

1

छवि को बिटमैप के रूप में लोड न करें, इसे किसी सरणी में परिवर्तित करें, फिर उसे भेजें।

बजाय

:

एक फ़ाइल, JPG प्रारूप में के रूप में यह पढ़ें। फ़ाइलों को एन्कोड करने के लिए बाइट सरणी का उपयोग करें, और फ़ाइल को भरें।

इसे बिटमैप पर लोड करना, अनावश्यक रूप से बड़ी मेमोरी समस्याओं का कारण बन रहा है। बिटमैप प्रारूप में दोहराई गई एक छवि, निरंतर की तुलना में ~ 20x या अधिक स्मृति लेगी।

सर्वर की ओर, आपको इसे फ़ाइल के रूप में भी इलाज करने की आवश्यकता होगी। बिटमैप के बजाए।

यहाँ बाइट के लिए एक फ़ाइल लोड करने के लिए एक लिंक [] है: Elegant way to read file into byte[] array in Java

+0

मुझे बिटमैप की आवश्यकता है, क्योंकि मुझे इसे आकार देने और इसे संपीड़ित करने की आवश्यकता है। मूल छवि का आकार 12 एमबी है और मैं लगभग 700 केबी सर्वर पर भेजना चाहता हूं, मूल छवि के 12 एमबी नहीं। – alasarr

+0

@alasarr आह मेरी गलती। आपको इसे गलत नमूना देना चाहिए। Http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Doomsknight

+0

@alasarr बस इतना ही पता है कि आप जानते हैं।बिटमैप प्रारूप में '2400x2400' छवि, प्रति पिक्सेल' 2 बाइट्स 'का उपयोग करके आकार में '11MB' है। इस पर ध्यान दिए बिना कि jpg प्रारूप '700KB' है या नहीं। या '4 बाइट' प्रतिनिधित्व में' 22 एमबी' – Doomsknight

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