【问题标题】:Load all images from assets folder dynamically动态加载资产文件夹中的所有图像
【发布时间】:2015-02-02 11:54:35
【问题描述】:

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

我的代码是这样的:

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

}

我不想拥有Integer[],而是想从资产子文件夹中拥有List<String> 或类似的东西

有什么想法吗???

【问题讨论】:

    标签: android arraylist android-assets


    【解决方案1】:

    有什么方法可以让我创建一个列表或ArrayList 资产子文件夹中的图片??

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

    String[] images =getAssets().list("images");
    ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));
    

    现在要在 imageview 中设置图像,您首先需要使用资产中的图像名称获取位图:

    InputStream inputstream=mContext.getAssets().open("images/"
                                          +listImages.get(position));
    Drawable drawable = Drawable.createFromStream(inputstream, null);
    imageView.setImageDrawable(drawable);
    

    【讨论】:

    • 它只是给了我一个黑色的网格视图
    • @KostasMatrix:调试您的代码并检查drawable 是否为空并检查。进入listImagesArrayList
    • @KostasMatrix: 你在assets 目录中添加了所有图像。在assets 的根目录或任何其他文件夹中?
    • 在名为 images 的 assets 子文件夹中
    • @KostasMatrix:查看我的编辑答案。需要在文件名之前使用文件夹名作为"images/"+listImages.get(position)
    【解决方案2】:

    String[] images =getAssets().list("images"); 返回 assets 中文件夹“images”的内容。

    如果您的资产文件夹直接包含您应该使用的图像

    String[] images =getAssets().list("");
    

    【讨论】:

      【解决方案3】:
      public static ArrayList<String> addAssetsImages(Context mContext, String folderPath) {
              ArrayList<String> pathList = new ArrayList<>();
              try {
                  String[] files = mContext.getAssets().list(folderPath);
                  for (String name : files) {
                      pathList.add(folderPath + File.separator + name);
                      Log.e("pathList item", folderPath + File.separator + name);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return pathList;
          }
      

      【讨论】:

        猜你喜欢
        • 2020-10-08
        • 1970-01-01
        • 2019-09-13
        • 2018-12-27
        • 2021-04-27
        • 1970-01-01
        • 2021-09-28
        • 1970-01-01
        • 2016-12-28
        相关资源
        最近更新 更多