如何显示所有图像从asssets文件夹(全部在网格中)?
问题描述:
我要显示所有图像驻留在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个图像。现在我想显示像网格图像的所有图像。
答
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();
}
得到的绘制列表中,并创建任何列表/回收适配器,然后ü可以显示所有图像
答
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;
}
}
你需要得到的图像阵列的大小和运行一个循环的大小 –
使用GridLayout经理的recyclerview。 –
https://stackoverflow.com/questions/28277210/load-all-images-from-assets-folder-dynamically – Ankita