java Android:File() 的资产文件夹路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13372500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:28:41  来源:igfitidea点击:

Android: path of assets folder for File()?

javaandroiddirectoryassets

提问by user1822613

I have some files in the assets folder of my project, and I want to list them, so I put this in my code:

我的项目的assets文件夹中有一些文件,我想列出它们,所以我把它放在我的代码中:

File dir = new File("com.packagename/assets/fonts");
File[] fileList = dir.listFiles();

Which path should I put to make it work? I want it so users could install new fonts (I don`t know how to do this yet) so I need to list all the fonts in the folder, including post-installed fonts. If there is any other solutions, please share.

我应该放哪条路径才能使它工作?我想要它以便用户可以安装新字体(我还不知道该怎么做)所以我需要列出文件夹中的所有字体,包括后安装的字体。如果有其他解决方案,请分享。

采纳答案by Jeremy Lyman

Assets and resources are accessible using file:///android_asset and file:///android_res.

资产和资源可以使用 file:///android_asset 和 file:///android_res 访问。

But in this case you will want to do something like this :

但在这种情况下,你会想要做这样的事情:

Resources res = getResources()
AssetManager am = res.getAssets();
String fileList[] = am.list(dirFrom);

if (fileList != null) {   
   for ( int i = 0;i<fileList.length;i++) {
        Log.d("",fileList[i]); 
    }
}