我想知道如何轮询安装应用程序以查找可用的资产文件夹.我已经打开了一些图标包并验证了那里有一个资产文件夹,它们充满了所有的图标png文件.
更新:
从下面的答案我已经编写了一些代码来尝试从我自己的项目资产目录中列出一个文件,但它似乎不起作用.
Resources r = this.getResources(); AssetManager a = r.getAssets(); String[] list = a.list("/"); Log.d("test","Length of / is "+list.length); for (String s : list) { Log.d("test",s); } Log.d("test","Length of /assets is "+a.list("/assets").length); Log.d("test","Length of /assets/ is "+a.list("/assets/").length); Log.d("test","Length of ./assets/ is "+a.list("./assets/").length); Log.d("test","Length of ./assets is "+a.list("./assets").length);
这是输出:
03-16 12:25:04.591: DEBUG/test(13526): Length of / is 6 03-16 12:25:04.591: DEBUG/test(13526): AndroidManifest.xml 03-16 12:25:04.591: DEBUG/test(13526): Meta-INF 03-16 12:25:04.591: DEBUG/test(13526): assets 03-16 12:25:04.591: DEBUG/test(13526): classes.dex 03-16 12:25:04.591: DEBUG/test(13526): res 03-16 12:25:04.591: DEBUG/test(13526): resources.arsc 03-16 12:25:04.614: DEBUG/test(13526): Length of /assets is 0 03-16 12:25:04.637: DEBUG/test(13526): Length of /assets/ is 0 03-16 12:25:04.661: DEBUG/test(13526): Length of /assets/ is 0 03-16 12:25:04.692: DEBUG/test(13526): Length of ./assets/ is 0 03-16 12:25:04.716: DEBUG/test(13526): Length of ./assets is 0
更新2 99%那里!!!:
我发现你可以从assets目录中读取而不实际使用文件夹名称:
InputStream is = assetManager.open("test.png");
我还尝试使用应用程序1中的应用2中的资源,其中文件夹路径为/asset/icon/image.png:
InputStream is = assetManager.open("icon/image.png");
接下来我发现你可以在资产中列出一个目录:
String[] list = assetManager.list("icons");
这也很有效.现在唯一失败的是如何列出基本目录资产.
解决方法
AssetManager am = this.getAssets(); String[] names = am.list("");
将列出一些其他文件:图像,声音,webkit,也许还有其他文件.您可以忽略这些目录,它们是框架资产目录的一部分.这是groups.google.com的引用:
Currently the asset manager merges the
asset directory from the framework
resources along with your own files
placed in “assets”. We should
probably change this behavior (it was
part of an old resource/ localization
model),but it doesn’t do much damage
except that you see more
files/directories in your own assets
than you might expect. Any of your
files that are the same as one in the
framework assets will be used instead,
when accessed through your
AssetManager.
您还可以在assets目录中列出子文件夹,不需要任何斜杠:
String[] names= am.list("subfolder");
请注意,我没有在文件名中包含“/ assets”.
最后,一旦有了文件列表,就可以加载它们:
InputStream in = am.open("file.png");
这将加载到基础资产文件夹中的文件中.或者您可以在子文件夹中加载文件,如下所示:
InputStream in = am.open("subfolder/file.png");
如果您需要将这些png加载到位图中,您还可以执行以下操作:
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);