如何显示asssets文件夹中的所有图像(全部在网格中)?

时间:2017-09-27 08:43:46

标签: android android-layout

我想显示驻留在Assets文件夹中的所有图像:我已经完成了一些代码:

func hide()

这显示了Assets中FolderA的1张图片。现在我想显示所有图像,如图像网格。

2 个答案:

答案 0 :(得分: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();
                }

获取drawable列表并创建任何列表/回收器适配器然后你可以显示所有图像

答案 1 :(得分: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;
        }
    }