javascript 如何从资产中确定特定文件的绝对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4744169/
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
How to determine the Absolute path for specific file from Assets?
提问by Sagar R. Kothari
In an iPhone Application, I just write one line to get the complete path for a specific file ( as below ).
在 iPhone 应用程序中,我只写一行来获取特定文件的完整路径(如下所示)。
NSString *strPath=[[NSBundle mainBundle] pathForResource:@"myjavascript" ofType:@"js"];
My question is How to get the complete path for a specific file which is located under Assets folder in Android.
我的问题是如何获取位于Android 中 Assets 文件夹下的特定文件的完整路径。
Sample Image.
示例图像。


回答by inazaruk
Assets are compiled into .apk file (which basically is zip file). You can't get system path into zip file. You have to manually unzip .apk to some folder and get file system path to assetfolder and files inside that folder.
资产被编译成 .apk 文件(基本上是 zip 文件)。您无法将系统路径放入 zip 文件。您必须手动将 .apk 解压缩到某个文件夹并获取资产文件夹和该文件夹内文件的文件系统路径。
But it sounds like you don't really need a path to asset files. You should look into these functions:
但听起来您并不真正需要资产文件的路径。您应该查看这些函数:
Context.getAssets()
AssetManager.open(String fileName)
AssetManager.openFd(String fileName)
Context.getAssets() 
AssetManager.open(String fileName) 
AssetManager.openFd(String fileName)
These functions allow you to get input stream or file descriptor for any of your assets. Here is an example:
这些函数允许您获取任何资产的输入流或文件描述符。下面是一个例子:
AssetManager assetManager = getAssets();
InputStream assetIn = assetManager.open("excanvas.js");
//do something with assetIn...  
You need to specify path to your asset relatively to assetfolder.
您需要指定资产相对于资产文件夹的路径。

