【问题标题】:how to print all file name present in assest folder .?如何打印资产文件夹中存在的所有文件名。?
【发布时间】:2014-07-30 04:27:44
【问题描述】:

我以这种方式评估其中有子文件夹和文件..

如图所示 ..assest->www ->js 或 cs 文件夹..then 文件

assest
 |
 |__www
     |
     |___js
     |    |
     |    |__a.js
     |
     |____cs
     |     |
     |     |__a.css
     |
     |____index.html

我需要打印所有文件名..我们可以打印那个文件名

我做了这个功能但不起作用..

package com.mobilecem.atms;

import java.io.IOException;

import android.content.Context;
import android.util.Log;

public class GlobalFunction {
    public static boolean listAssetFiles(String path,Context context) {

        String [] list;
        try {


            list = context.getAssets().list(path);
            if (list.length > 0) {
                // This is a folder
                for (String file : list) {
                    if (!listAssetFiles(path + "/" + file, context))
                        return false;
                }
            } else {
                Log.d("File Name", "file present");
                // This is a file
                // TODO: add file name to an array list
        } 

        }catch (IOException e) {
            return false;
        }

        return true; 
    }

}

这样打电话

GlobalFunction.listAssetFiles("", aaa.this);

结果: 列表有:index.html、a.js、css

评论

【问题讨论】:

  • 使用 AssetManager.list(String path) 获取给定路径下的所有文件
  • 我们可以不做一个通用函数意味着它给assest文件夹中的所有文件名
  • 我没明白,请你解释一下
  • @saleeh93 我只想要数组中assest文件夹中存在的所有文件名。正如我所说,在我的assest文件夹中有www文件夹然后文件夹中有js和cs文件夹有文件名称为 a.js 和 a.css。但在 www 中有 html 文件。我需要打印文件
  • 我需要获取文件夹中存在的所有文件名

标签: android


【解决方案1】:

给你 您将在文件列表中拥有所有文件及其子文件夹的详细信息 看看吧

List<String> files = new ArrayList<String>();

public boolean listAssetFiles(String path, Context context) {


    String[] list;
    try {


        list = context.getAssets().list(path);
        if (list.length > 0) {
            // This is a folder
            for (String file : list) {
                String root = TextUtils.isEmpty(path) ? "" : path + "/";
                String fullpath = root + file;
                files.add(fullpath);
                if (!listAssetFiles(root + file, context))
                    return false;
            }
        } else {

            // This is a file
            // TODO: add file name to an array list
        }

    } catch (IOException e) {
        return false;
    }

    return true;
}

【讨论】:

  • 让我检查一下等等
  • 这个函数怎么称呼?
  • 什么是路径参数?
  • listAssetFiles("", this);使用那个
  • 它只是展示了你需要修改为你的使用逻辑的工作概念
【解决方案2】:

这是完全可行的,并且显示了我刚刚测试过的文件夹名称

    private void listFiles(String dirFrom) throws IOException {
    Resources res = getResources(); // if you are in an activity
    AssetManager am = res.getAssets();
    String[] fileList = am.list(dirFrom);
    ArrayList<Drawable> dList = new ArrayList<>();

    if (fileList != null) {
        for (int i = 0; i < fileList.length; i++) {

            Log.d("FOLDER", fileList[i]);




    }
}

使用资产文件夹的根文件夹名称调用 listAssetFiles。

listAssetFiles("root_folder_name_in_assets");

如果根文件夹是资产文件夹,则调用它

    listAssetFiles("");

【讨论】:

    猜你喜欢
    • 2017-04-22
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多