[英]How to get the path to the Android assets folder in the application package
I have a whole folder structure that I want to copy from my assets folder.我有一个完整的文件夹结构,我想从我的资产文件夹中复制。 However, the mContext.getAssets().open() seems to only want a filename so that it can return an InputStream, which is only suitable for copying a single file.
但是,mContext.getAssets().open() 似乎只需要一个文件名,以便它可以返回一个 InputStream,它只适用于复制单个文件。 What I need is a File made from the folder in my assets folder so that I can recurse through all the files and folders and copy them all.
我需要的是从我的资产文件夹中的文件夹制作的文件,以便我可以递归所有文件和文件夹并将它们全部复制。
Does anybody know how to get the path to the assets folder so that I can create a File object?有谁知道如何获取资产文件夹的路径,以便我可以创建文件 object?
Edit: After some study it appears that you can't access files in the assets/ and raw/ folders with absolute paths to be able to create a File object.编辑:经过一些研究,您似乎无法使用绝对路径访问 assets/ 和 raw/ 文件夹中的文件,从而能够创建文件 object。 It probably has to do with the encryption of the app package.
它可能与应用程序 package 的加密有关。 I hope someone can prove me wrong though!
我希望有人能证明我错了!
Final edit: I ended up creating an array of strings to hold the extra asset files:最终编辑:我最终创建了一个字符串数组来保存额外的资产文件:
private static final String[] DEFAULT_ALBUM_FILES =
{INTRO_TO_FLASHUM_DIR+"03 Never Can Say Goodbye.m4a",
INTRO_TO_FLASHUM_DIR+"11 Bossa Baroque.m4a",
INTRO_TO_FLASHUM_DIR+"intro fling.3gp"};
I then iterated through this copying each file individually using the mContext.getAssets().open() to get the InputStream.然后,我使用 mContext.getAssets().open() 逐个复制每个文件以获取 InputStream。 I don't think it is currently possible to iterate through a folder in the assets using normal File operations.
我认为目前无法使用正常的文件操作遍历资产中的文件夹。
AssetManager am = con.getAssets();//u have get assets path from this code
InputStream inputStream = null;
inputStream = am.open("file.xml");
or或者
String file_name="ur.xml"
inputStream = am.open("foldername/"+ur);
Could you move the folder to your /raw folder?你能把文件夹移到你的 /raw 文件夹吗? Then you could use:
然后你可以使用:
com.your.package:raw/yourFile
Like this:像这样:
int resourceId = context.getResources().getIdentifier("com.your.package:raw/somefile.txt");
File f = new File(context.getResources().openRawResource(resourceId));
And here's someone doing it with the assets folder:这是有人用资产文件夹做的:
Android Assets with sub folders Android 带有子文件夹的资产
InputStream is = getAssets().open("subfolder/somefile.txt");
Use file:///android_asset
for accessing the asset folder and then you can always give your subfolder there.使用
file:///android_asset
访问资产文件夹,然后您可以随时将您的子文件夹放在那里。
AssetManager assetManager = null; // null ??? Get the AssetManager here.
AssetFileDescriptor assetFileDescriptor = null;
try{
assetFileDescriptor = assetManager.openFd("file:///android_asset/yourfolder/file");
FileDescriptor fd = assetFileDescriptor.getFileDescriptor();
} catch (Exception e){}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.