2017-09-27 123 views
0

我要顯示所有圖像駐留在Assets文件夾:我已經做了一些代碼:如何顯示所有圖像從asssets文件夾(全部在網格中)?

 mImageView = (ImageView) findViewById(R.id.grid_item_image); 
     // to reach asset 
     AssetManager assetManager = getAssets(); 
     // to get all item in dogs folder. 
     String[] images = assetManager.list("FolderA"); 

     InputStream inputStream = getAssets().open("FolderA/" + images[0]); 

     // load image as Drawable 
     Drawable d = Drawable.createFromStream(inputStream, null); 
     mImageView.setImageDrawable(d); 
     inputStream.close(); 

這說明我從FolderA在資產1個圖像。現在我想顯示像網格圖像的所有圖像。

+0

你需要得到的圖像陣列的大小和運行一個循環的大小 –

+0

使用GridLayout經理的recyclerview。 –

+0

https://stackoverflow.com/questions/28277210/load-all-images-from-assets-folder-dynamically – Ankita

回答

0
   mImageView = (ImageView) findViewById(R.id.grid_item_image); 
       // to reach asset 
       AssetManager assetManager = getAssets(); 
       // to get all item in dogs folder. 
       String[] images = assetManager.list("FolderA"); 
       for(int i=0;i<images.length;i++ 
       { 
       InputStream inputStream = getAssets().open("FolderA/" + images[i]); 

       // load image as Drawable 
       Drawable d = Drawable.createFromStream(inputStream, null); 
       // this will display only last image 
       mImageView.setImageDrawable(d); 

       inputStream.close(); 
       } 

得到的繪製列表中,並創建任何列表/回收適配器,然後ü可以顯示所有圖像

0
public final String ASSETS_BASE_PATH = "file:///android_asset"; 

    // store image name in arraylist. 
    if (myArrayList.size() > 0) { 
       GridAdapter myAdapter = new GridAdapter(MainActivity.this); 
       myGridView.setAdapter(myAdapter); 
    } 

// And inside get use Glide to load images to image View. 
// Add following dependency in app gradle.build file 
    compile 'com.github.bumptech.glide:glide:3.7.0' 


private class GridAdapter extends BaseAdapter { 
     ViewHolder holder; 
     private Context myContext; 
     private LayoutInflater mInflater; 

     public BannerAdapter(Context ctx) { 
      this.myContext = ctx; 
      this.mInflater = LayoutInflater.from(ctx); 
     } 

     @Override 
     public int getCount() { 
      return myArrayList.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return myArrayList.get(position); 
     } 

     public String getItemValue(int position) { 
      return myArrayList.get(position).designName; 
     } 

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

     @Override 
     public View getView(final int position, View convertView, ViewGroup viewGroup) { 

      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.item_grid, null); 
       holder = new ViewHolder(); 
       holder.myImageView= (ImageView) convertView.findViewById(R.id.imageView); 
       convertView.setTag(holder); 
       holder.positionViewHolder = position; 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      String url = myArrayList.get(position); 

      String glideLoadUrl = url; 

      glideLoadUrl = ASSETS_BASE_PATH + "/" + url; 

      Glide.with(MainActivity.this).load(glideLoadUrl).into(holder.myImageView); 
      return convertView; 
     } 
    } 
相關問題