Android中assets各级目录的遍历及小结

一、文件特性

  • 单个文件需要小于1M
  • 文件只能读,不能做写操作
  • 自带隐藏的3个文件夹资源:images 、sounds 、webkit
  • 不会被映射到R中,不能通过R.XXX.ID的方式访问,仅能通过AssetManager读取
  • 打包进apk时,不进行压缩,可以有多级目录(raw文件夹也不压缩,不可以有多级目录,会被映射到R中,放置多媒体文件)
  • APK安装之后会放在"/data/app/**.apk"中,并不是一个目录,所以不能通过File操作。对数据的操作需要通过AssetsManger.open()方法得到流,然后就是对java流的操作。而对于其它的目录,如files位于"/data/data/package_name/files",是可见、有方法获取的

二、目录示例

三、遍历方法

/**
 * 遍历assets文件夹
 *
 * @param tab  格式化显示占位符
 * @param path 在assets中的路径
 */
private void traverseAssets(String tab, String path) {
    AssetManager assetManager = getAssets();

    try {
        // 获取path目录下,全部的文件、文件夹
        String[] list = assetManager.list(path);

        if (list == null || list.length <= 0) {
            // 当前为文件时,或者当前目录下为空
            return;
        }

        for (int i = 0; i < list.length; i++) {
            System.out.println(tab + list[i]);

            String subPath;
            if ("".equals(path)) {
                // 如果当前是根目录
                subPath = list[i];
            } else {
                // 如果当前不是根目录
                subPath = path + "/" + list[i];
            }

            traverseAssets(tab.concat("___"), subPath);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

四、调用代码

// 从assets根目录开始遍历
traverseAssets("", "");

五、遍历结果

I/System.out: 1.txt
I/System.out: one
I/System.out: ___2.txt
I/System.out: ___two
I/System.out: ______3.txt
I/System.out: images
I/System.out: ___android-logo-mask.png
I/System.out: ___android-logo-shine.png
I/System.out: sounds
I/System.out: ___bootanim0.raw
I/System.out: ___bootanim1.raw
I/System.out: webkit
I/System.out: ___android-weberror.png
I/System.out: ___hyph_en_US.dic
I/System.out: ___incognito_mode_start_page.html
I/System.out: ___missingImage.png
I/System.out: ___nullPlugin.png
I/System.out: ___play.png
I/System.out: ___textAreaResizeCorner.png
I/System.out: ___togglePlugin.png
I/System.out: ___youtube.html
I/System.out: ___youtube.png

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值