在研究如何遍历Assets下的文件时,我得到了一点奇怪的结果 - 看到了一些本不属于当前应用的文件结构。

首先贴一下遍历代码

public static void printAssets(Context c) {AssetManager am = c.getAssets();printAssetsFiles(am, "", "");
}public static void printAssetsFiles(AssetManager am, String parent, String current, String indent) {
if (!TextUtils.isEmpty(current)) {
System.out.println(indent + current);
indent += "\t";
}
// 列出子文件
String[] files;
String currentParent;
try {
if (TextUtils.isEmpty(parent)) {
currentParent = current;
} else {
currentParent = parent + "/" + current;
}
files = am.list(currentParent);
} catch (IOException e1) {
return;
}
if (files != null && files.length > 0) {
for (String f : files) {
printAssetsFiles(am, currentParent, f, indent);
}
}
}

下面是在我手机上跑出来的结果,运行环境是小米4,Android4.4.4

MyFile1.txt
MyFile2.txt
MyFolder1MyFile1.txtMySubFolder1MyFile1-1.txt
MyFolder3MyFile2.txt
MyFolder4MySubFolder4MyFile1.txt
device_featuresH2X_I18N.xmlHM2013022.xmlHM2013023.xmlHM2014011.xmlHM2014112.xmlHM2014501.xmlHM2014811.xmlHM2014812.xmlHM2014813.xmlHM2014817.xmlHM2014818.xmlHM2014819.xmlHM2014821.xmlaries.xmlarmani.xmlcancro_MI3.xmlcancro_MI4.xmldior.xmlferrari.xmlgucci.xmlhammerhead.xmllcsh92_wet_jb9.xmllcsh92_wet_tdd.xmlleo.xmllte26007.xmlmocha.xmlpisces.xmltaurus.xmlvirgo.xml
huangli.idf
imagesandroid-logo-mask.pngandroid-logo-shine.png
licensebrpt_BReula.htmlprivacy.htmldefaulten_UScopyright.htmleula.htmlmibilicense.htmlmiclouduseragreement.htmprivacy.htmlzh_CNcopyright.htmleula.htmlmibilicense.htmlmiclouduseragreement.htmprivacy.htmlzh_TWcopyright.htmleula.htmlmiclouduseragreement.htmprivacy.htmlsgen_USprivacy.html
pinyinindex.idf
soundsbootanim0.rawbootanim1.raw
telocation.idf
webkitandroid-weberror.pnghyph_en_US.dicincognito_mode_start_page.htmlmissingImage.pngnullPlugin.pngplay.pngtextAreaResizeCorner.pngtogglePlugin.pngyoutube.htmlyoutube.png

东西很多,但是在我应用的Assets下的文件夹结构是这样的:

MyFile1.txt
MyFile2.txt
MyFolder1MyFile1.txtMySubFolder1MyFile1-1.txt
MyFolder3MyFile2.txt
MyFolder4MySubFolder4MyFile1.txt

在这里我们会发现,输出列表中包含大量的文件,这些文件都不是我们应用中的。

(在这里说两句题外话,输出列表中是没有空目录的,是因为apk在打包的时候把空目录过滤了,大家可以用压缩软件打开看看)

那么多出来的文件都是从哪里来的呢?我们去扒一扒AssetManager的源码,先从list方法开始:

/*** Return a String array of all the assets at the given path.* * @param path A relative path within the assets, i.e., "docs/home.html".* * @return String[] Array of strings, one for each asset.  These file*         names are relative to 'path'.  You can open the file by*         concatenating 'path' and a name in the returned string (via*         File) and passing that to open().* * @see #open*/public native final String[] list(String path)throws IOException;

这是个本地的方法,虽然没有源码,但是从注释中也能看出来,“all the assets”说明这里不是只有一个Assets目录的,那么除了我们自己的apk,肯定不能是其他人的apk啊,那就只能是系统的Assets了。

再翻代码,发现AssetManager中有个静态的实例sSystem,看起来这就是系统的Assets了。这个类中有一个getSystem方法,是获取该实例的。这是个隐藏方法,我们可以用反射获取:

Method getSystem = AssetManager.class.getDeclaredMethod("getSystem");
AssetManager am = (AssetManager) getSystem.invoke(null);

遍历这个AssetManager的目录,可以得到上面多出来的文件列表。至于这个Assets文件的位置在哪里,我没有找到,有兴趣的读者可以找找。

另外,如果apk Assets下的文件路径和系统中的文件路径相同,那么在读取的时候,apk自己的文件会覆盖系统文件,所以不需要担心此问题。

最后,值得一提的是,如果我么使用如下参数调用上面的方法:

printAssetsFiles(am, "/", "");

得到的将是apk压缩文件的根目录结构如下:

AndroidManifest.xml
META-INF
assets
classes.dex
lib
res
resources.arsc

此时使用am.open打开文件,只能得到异常。不过对于文件,比如AndroidManifest.xml,可以使用AssetManager的隐藏api方法openNonAsset打开,但是对于目录就无能为力了,当然,API是不建议用户用AssetManager打开这个文件的(所以文档中称呼这些文件为NonAsset文件)

CyanFlxy原创,转载请注明出处。

Android应用中遍历Assets的结果相关推荐

  1. Android项目中的assets和raw文件夹

    直接参考这篇文章 Assets文件夹和raw文件夹的区别 转载于:https://www.cnblogs.com/feng-ye/p/7026594.html

  2. android+assets+在哪,我在哪里将’assets’文件夹放在Android Studio中?

    我对assets文件夹感到困惑. 它不是在Android Studio中自动创建的,几乎所有论坛都讨论了Eclipse. 如何在Android Studio中配置Assets目录? 由于Android ...

  3. uni-app如何打包apk到Android studio中调用

    首先我们知道,Android和uni-app中写的代码是完全不一样的,他们的编译环境也是不一样的,呢么如何要在Androidstudio中调用uni-app中的代码呢?我们这边来准备几个步骤请看: 第 ...

  4. Android Studio中新建和引用assets文件

    从eclipse转过的朋友们应该不太习惯AS中新建assets文件和对文件内容的引用.我也查找了网上很多资料发现很少有这样的解决答案,于是便把自己解决的方法总结在这里. 1.一般新建project后这 ...

  5. android遍历的方法,android中遍历arrayList的四种方法

    一.在android中遍历arrayList有以下四种方法: 1.实例: package com.mylist.test; import java.util.ArrayList; import jav ...

  6. android assets 在哪里,轻读一下 Android 应用开发中的 assets 目录

    2019-08-07 关键字:APK预置文件.预置配置文件.res,raw与assets的区别 在Android的应用开发中,难免会遇到外部文件的预置需求.例如图像.音视频.配置文件.字体等等.对于图 ...

  7. android 遍历实体类,Java中遍历实体类(处理MongoDB)

    在实际过程中,经常要将实体类进行封装,尤其是处理数据库的过程中:因此,对于遍历实体类能够与数据库中的一行数据对应起来. 我是使用的环境是Spring boot,访问的数据库时MongoDB 实体类遍历 ...

  8. android打开wav格式,FileNotFoundException从Android资产中打开wav文件

    在我们的android应用程序中,我们打开位于assets/config/notification.wav中的wav文件.要打开和播放声音,我们使用下面的代码:FileNotFoundExceptio ...

  9. Android开发中应避免的重大错误

    by Varun Barad 由Varun Barad Android开发中应避免的重大错误 (Critical mistakes to avoid in Android development) A ...

最新文章

  1. Android init.rc文件解析过程详解(二)
  2. 贝叶斯神经网络计算核裂变碎片产额
  3. 内网通广告弹窗怎么关掉_人民日报批弹窗广告!“弹窗广告”怎么关闭 弹窗广告去除拦截方法...
  4. HDU OJ 动态规划46题解析
  5. 数据结构与算法--死磕二叉树
  6. 久谦咨询python笔试题目_python笔试含答案
  7. SQL结构化查询语言基础知识 转
  8. Harmony OS — TextField输入框
  9. 遍历查询+从非根节点开始遍历+从下向上遍历树+从层次化查询中删除节点和分支...
  10. Nifi Api访问
  11. python的认识从唯物主义_中国大学mooc用Python玩转数据章节答案
  12. Express中间件
  13. qq邮箱怎么发送html文件在哪里,QQ邮箱怎么发送文件夹
  14. 程序员情人节脱单指南
  15. 上班族难懂五险一金 交的越多是否越吃亏
  16. Qt中绘制五子棋棋盘
  17. 垃圾收集器垃圾回收算法知识图解
  18. android五大布局的作用,Android五大布局与实际应用详解
  19. 【编程练习】3*3 的矩阵,值限定为1-9不重复,已知横竖的和,和一个斜着的值,求这个矩阵
  20. 简报 | 俄罗斯为离岸地区制定特殊加密货币规则

热门文章

  1. 好玩的中性灰--绝对中性灰128:128:128
  2. 采用用计算机及条形码技术的是什么,条码技术在计算机应用与实践
  3. 如何去实践一个完整的数据挖掘项目?
  4. TP-Link路由无线WDS桥接
  5. [转]苹果商店审核规则,你触犯了哪一条?
  6. 借大数据“互联网+”整治交通违法
  7. C# 设置label(标签)控件的背景颜色为透明
  8. linux有的wifi搜不到网络,怎么解决安装了Ubuntu后发现没有无线网络,搜索不到WiFi的情况?...
  9. 网络安全之浏览器信息伪造
  10. java乐器_用java新建一个乐器类,包含属性:名称、重量、品牌、价格;包含方法:不带参数的构造方法、表演方法...