2010-05-02 10 views
9

में संपत्ति फ़ोल्डर से छवियां मैं छवियों के ग्रिड व्यू को बनाने पर काम कर रहा हूं, छवियों के साथ संपत्ति फ़ोल्डर में मौजूद हैं। Opening a File from assets folder in android लिंक ने इसे पढ़ने के लिए बिटमैप का उपयोग करने में मेरी सहायता की। कोड वर्तमान में हो रहा है है:एंड्रॉइड - ग्रिड व्यू

public View getView(final int position, View convertView, ViewGroup parent) 
{ 

    try 
    { 
    AssetManager am = mContext.getAssets(); 
    String list[] = am.list(""); 
    int count_files = imagelist.length; 
    for(int i= 0;i<=count_files; i++) 
    { 
     BufferedInputStream buf = new BufferedInputStream(am.open(list[i])); 
     Bitmap bitmap = BitmapFactory.decodeStream(buf); 
     imageView.setImageBitmap(bitmap); 
     buf.close(); 
    } 
    } 
    catch (IOException e) 
    { 
    e.printStackTrace(); 
    } 
    } 

मेरा आवेदन आस्तियों फ़ोल्डर से छवि को पढ़ने करता, लेकिन यह ग्रिड दृश्य में कोशिकाओं के माध्यम से पुनरावृत्ति नहीं है। ग्रिड व्यू की सभी कोशिकाओं में छवियों के सेट से एक ही छवि चुनी गई है। क्या कोई मुझे बता सकता है कि कोशिकाओं के माध्यम से पुनरावृत्ति कैसे करें और अभी भी अलग-अलग छवियां हों?

मैं एक ImageAdapter कक्षा जो BaseAdapter वर्ग फैली में उपरोक्त कोड है, और अपने मुख्य वर्ग में मैं अपने gridview साथ लिंकिंग हूँ द्वारा:

GridView gv =(GridView)findViewById(R.id.gridview); 
    gv.setAdapter(new ImageAdapter(this, assetlist));  

धन्यवाद एक बहुत, अग्रिम में किसी भी मदद के लिए सरन

+0

क्या कोई विशेष कारण है कि आप उन्हें आकर्षित करने वाले संसाधनों के बजाय 'संपत्ति /' में डाल रहे हैं? – CommonsWare

+0

असल में मैं इन छवियों को गतिशील रूप से लोड करना चाहता हूं, और वे संख्या में बहुत से हैं। जब मैंने उन्हें संसाधनों में रखा था, तो मुझे स्मृति समस्या थी। सामग्री प्रदाता को थोड़ा सा हाथ मिल रहा था, इसलिए मैंने सोचा कि मैं संपत्तियों में कोशिश करूँगा। - सरन – Saran

उत्तर

3

हर बार सभी वस्तुओं को पढ़ने की आवश्यकता नहीं है। GetView विधि कॉल में दी गई स्थिति पर केवल आइटम पढ़ें। और उस समय केवल उस आइटम को प्रदर्शित करें।

BufferedInputStream buf = new BufferedInputStream(am.open(list[position])); 
19

सरन, नीचे मैं गैलरी के साथ संपत्ति फ़ोल्डर में छवियों को दिखाने के लिए उपयोग करता हूं। मुझे लगता है कि यह ग्रिडव्यू के साथ एक ही सौदा है:

public class myActivitye extends Activity 
{ 
    private Gallery mGallery; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mGallery = (Gallery) findViewById(R.id.mygalleryinxml); 

     //load images into memory 
     mBitArray = new Bitmap[4]; 
     try 
     { 
      //these images are stored in the root of "assets" 
      mBitArray[0] = getBitmapFromAsset("pic1.png"); 
      mBitArray[1] = getBitmapFromAsset("pic2.png"); 
      mBitArray[2] = getBitmapFromAsset("pic3.png"); 
      mBitArray[3] = getBitmapFromAsset("pic4.png"); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

     mGallery.setAdapter(new GalleryAdapter(this, mBitArray)); 
    } 

    public class GalleryAdapter extends BaseAdapter 
    { 
     //member variables 
     private Context mContext; 
     private Bitmap[] mImageArray; 

     //constructor 
     public GalleryAdapter(Context context, Bitmap[] imgArray) 
     { 
      mContext = context; 
      mImageArray = imgArray; 
     } 

     public int getCount() 
     { 
      return mImageArray.length; 
     } 

     public Object getItem(int position) 
     { 
      return position; 
     } 

     public long getItemId(int position) 
     { 
      return position; 
     } 

     //returns the individual images to the widget as it requires them 
     public View getView(int position, View convertView, ViewGroup parent) 
     { 
      final ImageView imgView = new ImageView(mContext); 

      imgView.setImageBitmap(mImageArray[position]); 

      //put black borders around the image 
      final RelativeLayout borderImg = new RelativeLayout(mContext); 
      borderImg.setPadding(20, 20, 20, 20); 
      borderImg.setBackgroundColor(0xff000000);//black 
      borderImg.addView(imgView); 
      return borderImg; 
     } 

    }//end of: class GalleryAdapter 


    /** 
    * Helper Functions 
    * @throws IOException 
    */ 
    private Bitmap getBitmapFromAsset(String strName) throws IOException 
    { 
     AssetManager assetManager = getAssets(); 

     InputStream istr = assetManager.open(strName); 
     Bitmap bitmap = BitmapFactory.decodeStream(istr); 
     istr.close(); 

     return bitmap; 
    } 
} 
+0

वैसे, क्योंकि आप छवियों को गतिशील रूप से लोड करना चाहते हैं (मैंने उन्हें अपने उदाहरण में हार्डकोड किया है), अगर मैं आप थे तो मैं छवियों को "संपत्ति" के फ़ोल्डर में रखूंगा और फिर am.list (" [आपका फ़ोल्डर नाम] "); ऐसा इसलिए है क्योंकि मैंने अपनी फ़ाइलों के अलावा एंड्रॉइड फाइलों को संपत्तियों के भीतर देखा है –

+1

विधि getBitmapFromAsset() इनपुटस्ट्रीम को बंद करने के लिए उपेक्षा कर रहा है। –

+0

धन्यवाद !! मैंने अपना नमूना कोड अपडेट किया –