问题描述
我正在构建一个应用程序,该应用程序当前从drawable文件夹读取图像.由于我在drawable中没有子文件夹,因此我必须在assets文件夹中创建子文件夹并从那里加载图像.我可以用任何方法用资产子文件夹中的所有图像创建List
或ArrayList
吗?
I am building an application that currently read images from the drawable folder. Since i cannot have subfolders in the drawable i have to create subfolders in the assets folder and load images from there. Is there any way that i can create a List
or an ArrayList
with all the images from the assets subfolder??
我的代码是这样的:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9, R.drawable.pic_10,
R.drawable.pic_11, R.drawable.pic_12,
R.drawable.pic_13, R.drawable.pic_14,
R.drawable.pic_15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(250, 400));
return imageView;
}
}
我希望从资产子文件夹中获得一个List<String>
或类似的内容,而不是一个Integer[]
Instead of having an Integer[]
i want to have a List<String>
or something like this from the assets subfolder
任何想法?
推荐答案
是的,在assets
目录中创建子文件夹.使用getAssets().list(<folder_name>)
从资产中获取所有文件名:
Yes, create sub-folder in assets
directory. use getAssets().list(<folder_name>)
for getting all file names from assets:
String[] images =getAssets().list("images");
ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));
现在要在imageview中设置图像,您首先需要使用资产中的图像名称获取位图:
Now to set image in imageview you first need to get bitmap using image name from assets :
InputStream inputstream=mContext.getAssets().open("images/"
+listImages.get(position));
Drawable drawable = Drawable.createFromStream(inputstream, null);
imageView.setImageDrawable(drawable);
这篇关于动态加载资产文件夹中的所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!