2012-07-08 26 views
13

मेरे कोड कोड है:MediaStore.Images.Media.getBitmap और स्मृति त्रुटि से बाहर

public Bitmap loadPhoto(Uri uri) { 
    Bitmap scaled = null; 
    try { 
    scalled = Bitmap.createBitmap(
     MediaStore.Images.Media.getBitmap(getContentResolver(), uri), 
     0,0,90, 90); 

    if (scaled == null) { return null; } 
    } catch(Exception e) { } 
    return scaled; 
} 

इस के बाद। मैं ImageView में स्केल किया गया। प्रत्येक छवि डिवाइस कैमरा से आता है।

हर बार, मुझे त्रुटि मिलती है: स्मृति से बाहर कैमरे से तीन फ़ोटो प्रदर्शित करने के बाद। इसे कैसे हल करें?

+1

मैं एक ही समस्या थी, इस लिंक की जाँच: [http://tutorials-android.blogspot.co.il/2011/11/ आउटफमेमरी-अपवाद-कब-डीकोडिंग.html] (http://tutorials-android.blogspot.co.il/2011/11/outofmemory-exception-when-decoding.html) – zwebie

+1

बिटमैप स्केलिंग पर चेत हास का अच्छा ट्यूटोरियल भी देखें: http://www.youtube.com/watch?v=12cB7gnL6po –

+0

जागरूक रहें *** यह पृष्ठ DATE से बहुत दूर है *** - आज आप बस ऐसा करते हैं: http://stackoverflow.com/a/24135522/ 2 9 4884 – Fattie

उत्तर

1

MediaStore.getBitmap विधि एक सुविधा की विधि जब बिटमैप प्राप्त है कि एक नमूने का आकार निर्दिष्ट नहीं करता है ठीक करने के लिए प्रयास करें। यदि आप GetBitmap (ContentResolver, Uri) का उपयोग कर रहे हैं, और नमूना आकार का उपयोग करना चाहते हैं, तो इनपुट स्ट्रीम प्राप्त करने के लिए केवल ContentResolver का उपयोग करें, और सामान्य रूप से बिटमैप को डीकोड करें (पहले नमूना आकार की गणना करना, और उसके बाद इसे उपयुक्त के साथ लोड करना नमूने का आकार)।

+1

एक अच्छा दृष्टिकोण की तरह लगता है, इसके लिए कोड बेहतर जवाब देगा – weston

+0

@ वेस्टर्न Google इस नमूना कोड (कुशलतापूर्वक बिटमैप्स लोड करने के संबंध में) [लिंक] पर प्रदान करता है (http://developer.android.com/training/displaying-bitmaps/load-bitmap.html) ContentResolver का उपयोग देखकर पाया जा सकता है MediaStore.getBitmap के कार्यान्वयन पर। यह कंटेंट रिज़ॉल्वर को उसी तरह से कॉल करता है जैसे –

+1

मुझे पता है कि आप कहीं और जवाब पा सकते हैं, क्योंकि मुझे पहले से ही ऐसा करना पड़ा क्योंकि उत्तर अपूर्ण है। जो भी इस जवाब को पढ़ता है उसे वही पैर काम करना होगा। एक अच्छा स्टैक ओवरफ़्लो उत्तर इसे पूरा करने के लिए बाहरी लिंक पर भरोसा नहीं करता है। – weston

2

जो कोड नमूने के लिए देख रहे हैं उन लोगों के लिए:

private 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) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

public static Bitmap decodeSampledBitmapFromUri(Context context, Uri imageUri, int reqWidth, int reqHeight) throws FileNotFoundException { 

    // Get input stream of the image 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    InputStream iStream = context.getContentResolver().openInputStream(imageUri); 

    // First decode with inJustDecodeBounds=true to check dimensions 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(iStream, null, options); 

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

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

क्या आपने वास्तव में इस फ़ंक्शन को आजमाया है? मैंने कोशिश की और कार्य decodeSampledBitmapFromUri हमेशा शून्य लौटता है। और काफी लंबे शोध के बाद मैंने पाया कि आप उसी इनपुटस्ट्रीम का उपयोग बिटमैपफैक्टरी.decodeStream को पैरामीटर के रूप में एक से अधिक बार नहीं कर सकते हैं। – Dika