2010-05-02 37 views
9

我一直在創建圖像的網格視圖,圖像出現在Assets文件夾中。 Opening a File from assets folder in android鏈接幫助我使用位圖來讀取它。我目前遇到的代碼是:Android - 來自GridView中Assets文件夾的圖像

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

我的應用程序從Assets文件夾中讀取的圖像,但它不是通過在網格視圖中的細胞進行迭代。網格視圖的所有單元格都具有從該組圖像中選取的相同圖像。任何人都可以告訴我如何遍歷單元格,並仍然有不同的圖像?

我有一個ImageAdapter類擴展了BaseAdapter類上面的代碼,並在我的主類,我鏈接,與我的GridView的:

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

非常感謝提前任何幫助, Saran

+0

是否有一個特定的原因,你把這些在資產/'而不是作爲drawable資源? – CommonsWare 2010-05-02 13:09:18

+0

其實我想要動態加載這些圖片,而且它們的數量很多。當我擁有資源時,我有記憶問題.ContentProvider有點失控,所以我想我會試用資產。 - Saran – Saran 2010-05-02 16:29:04

回答

3

無需每次都讀取所有項目。只讀取getView方法調用中給定位置的項目。並且只顯示那個項目。

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

Saran,下面是我用來顯示資源文件夾中的圖像與畫廊。我想這是一個GridView相同的交易:

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

順便說一句,因爲你想要動態加載圖像(我把它們硬編碼在我的例子中),如果我是你,我會把圖像放在「assets」文件夾中,然後使用am.list(「 [你的文件夾名稱]「);這是因爲除了我自己的文件外,我還看到了資產中的Android文件 – 2011-05-05 19:42:37

+1

方法getBitmapFromAsset()忽略關閉它打開的InputStream。 – 2012-12-10 20:07:22

+0

謝謝!我更新了我的示例代碼 – 2012-12-17 17:22:26

相關問題