2011-11-20 25 views
6

काम नहीं कर रहा है मुझे लगता है कि मेरे ImageView में कोई समस्या है। मैं गैलरी, जहां मैं नीचे मेरी imageView में एक छवि को छूने और यह डाल सकते हैं बनाया:एक छवि दृश्य में एक बड़ी छवि डालें

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/fonddegrade"> 

<Gallery android:layout_height="wrap_content" 
    android:id="@+id/gallery" 
    android:layout_width="fill_parent" /> 

<ImageView android:layout_below="@+id/gallery" 
    android:layout_height="wrap_content" 
    android:id="@+id/laphoto" 
    android:layout_width="wrap_content" 
    android:layout_centerHorizontal="true"/> 

यह पूरी तरह से छोटी छवि के साथ काम कर रहा है, लेकिन नहीं बड़ी छवि (3264 * 1952) के साथ। जब मैं इसे स्पर्श करता हूं (इसलिए, इसे ImageView में डालने का प्रयास कर रहा है), मुझे एक त्रुटि है और मेरा एप्लिकेशन क्रैश है। यहाँ छवि प्रदर्शित करने के लिए अपने जावा कोड है:

 public void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
      this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setContentView(R.layout.photo); 

      File images; // Trouver le bon endroit de stockage 
      if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) 
       images = new File("/sdcard/MyPerformance/Photo"); 
      else 
       images = this.getFilesDir(); 

      images.mkdirs(); 
      File[] imagelist = images.listFiles(new FilenameFilter(){ 
       @Override 
       public boolean accept(File dir, String name) 
       { 
        return ((name.endsWith(".jpg"))||(name.endsWith(".png"))); 
       } 
      }); 

      mFiles = new String[imagelist.length]; 
      for(int i = 0 ; i< imagelist.length; i++) 
      { 
       mFiles[i] = imagelist[i].getAbsolutePath(); 
      } 
      mUrls = new Uri[mFiles.length]; 
      for(int i = 0; i < mFiles.length; i++) 
      { 
       mUrls[i] = Uri.parse(mFiles[i]);  
      } 

      imgView = (ImageView)findViewById(R.id.laphoto); 
      if(mFiles.length != 0) 
       imgView.setImageURI(mUrls[0]); 

      gallery = (Gallery) findViewById(R.id.gallery); 
      gallery.setAdapter(new ImageAdapter(this)); 

      gallery.setOnItemClickListener(new OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
        imgView.setImageURI(mUrls[position]); 
       } 
      }); 
    } 

या तो समस्या (लेकिन मुझे नहीं लगता कि यह कारण है, क्योंकि यह छोटी छवि के साथ काम है) या आकार की वजह से setImageURI से आता है चित्र।

इस समस्या को हल करने के लिए आप मुझे क्या समाधान दे सकते हैं? आपका धन्यवाद है।

पीएस: क्यों मेरा "हैलो" हमेशा हटा दिया जाता है?

+0

तो समस्या क्या है? क्या यह दुर्घटनाग्रस्त है? बीटीडब्ल्यू, आपकी छवि वास्तव में बड़ी दिखती है, एंड्रॉइड इसे लोड करते समय स्मृति से बाहर हो सकता है। –

+0

पूर्व छवि का आकार बदलें - यह एक सुंदर सुअर है। एक वाणिज्यिक उत्पाद http://www.avs4you.com/AVS-Image-Converter.aspx – mozillanerd

+0

इसके अलावा, अगर आप लॉगकैट में क्रैश त्रुटि की जांच करते हैं, तो यह आपको एक सुराग देगा ?? कृपया ऐसा करें, और यदि संदेह में अभी भी त्रुटि पोस्ट करें। – Peterdk

उत्तर

8

आपकी छवि शायद एंड्रॉइड के लिए बहुत बड़ी है और यह स्मृति से बाहर हो गई है। ऐप्स के पास 16 एमबी उपयोग योग्य मेमोरी जितनी कम हो सकती है। आपकी छवि 3264 * 1952 * 4 = ~ 25.5 एमबी (चौड़ाई ऊंचाई argb लेती है)। तो छवियों को छोटे आकार में आकार देने के लिए सबसे अच्छा हो सकता है।

देखें: http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
तब: Strange out of memory issue while loading an image to a Bitmap object
अंत: VM running out of memory while getting images from the cache

+2

यह है! आपके लिंक ने मुझे बहुत मदद की, मेरी समस्या दूसरी लिंक के समान थी, और मैंने उस समस्या से छुटकारा पाने के लिए एक ही समाधान का उपयोग किया। इसलिए, हमें अच्छे काम के लिए बहुत बड़ी छवि स्केल करना है। – FR073N

+0

मुझे खुशी है कि उन्होंने काम किया :) एक समान मुद्दे की तरह माना जाता है। –

+0

(उत्तर भी स्वीकार करना याद रखें) –

1

यहाँ बड़ी बिटमैप्स की हैंडलिंग के साथ आपकी सहायता के लिए एक बिटमैप [वर्ग util है।

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 

public class BitmapUtils { 

    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 decodeSampledBitmapFromResource(String pathToFile, 
       int reqWidth, int reqHeight) { 

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

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

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

} 
0

मैंने घंटों की खोज की है और सभी लिंक के माध्यम से चले गए हैं और अंततः मुझे यह मिला।

ग्लाइड.with (getContext()) .load (चयनित इमेजयूरी) .into (imageView);

और ग्लाइड सभी बैकएंड काम करता है। इसने मेरा दिन बना दिया।

ग्लाइड Github लिंक: https://github.com/bumptech/glide

+0

हालांकि यह लिंक प्रश्न का उत्तर दे सकता है, लिंक केवल लिंक को स्टैक ओवरफ़्लो पर हतोत्साहित किया जाता है, आप लिंक के महत्वपूर्ण हिस्सों को ले कर और इसे अपने उत्तर में डालकर इस उत्तर को बेहतर बना सकते हैं, यह सुनिश्चित करता है यदि लिंक बदल जाता है या हटा दिया जाता है तो आपका उत्तर अभी भी एक उत्तर है :) – WhatsThePoint

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