动态加载资产文件夹中的所有图像 [英] Load all images from assets folder dynamically

查看:36
本文介绍了动态加载资产文件夹中的所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个当前从 drawable 文件夹中读取图像的应用程序.由于我不能在 drawable 中创建子文件夹,因此我必须在资产文件夹中创建子文件夹并从那里加载图像.有什么方法可以使用资产子文件夹中的所有图像创建 ListArrayList 吗?

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 或来自 assets 子文件夹的类似内容,而不是 Integer[]

Instead of having an Integer[] i want to have a List<String> or something like this from the assets subfolder

有什么想法吗???

推荐答案

有什么方法可以创建一个 List 或一个 ArrayList来自资产子文件夹的图像??

Is there any way that i can create a List or an ArrayList with all the images from the assets subfolder??

是的,在 assets 目录中创建子文件夹.使用 getAssets().list() 从资产中获取所有文件名:

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

这篇关于动态加载资产文件夹中的所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆