22

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. 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?

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. It probably has to do with the encryption of the app 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. I don't think it is currently possible to iterate through a folder in the assets using normal File operations.

2
  • What have you tried? See: wiseandroid.com/post/2010/06/14/… Commented May 2, 2011 at 18:55
  • Yes, this describes exactly what I did. Commented May 23, 2011 at 13:58

3 Answers 3

5

Could you move the folder to your /raw folder? 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

 InputStream is = getAssets().open("subfolder/somefile.txt"); 
Sign up to request clarification or add additional context in comments.

6 Comments

This looks promising. I'm going to give it a try. I'll get back with the results. Thanks!
The raw folder also does not appear to allow absolute path references. As least I have not been able to figure it out...
The problem is that openRawResource() returns an InputStream and there does not seem to be a way to convert the latter to a File. With your code I get the following error: The constructor File(InputStream) is undefined.
Ah thats because I short-cut this answer :-) Once you have the IS, you then create your File and loop round the IS feeding it into the file. This might not be the best example: roseindia.net/java/java-conversion/InputstreamToFile.shtml but gives you a hint
Thanks for your help. I am aware of this technique to convert an IS to a File. However, how do I get the File that correspondes to a folder in the assets folder? From there I wish to do a file.listFiles() to get the contents of that folder and iterate through it to copy the individual files. As it is, I have to know the contents of that folder and copy the files individually.
|
5
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); 

2 Comments

Hi, is there way to get a file's path from InputStream object?
@SergeyYu, I too, have the same question. So far, I've not been able get the path from the stream. Is this solved?
-3

Use file:///android_asset for accessing the asset folder and then you can always give your subfolder there.

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){} 

4 Comments

I am afraid I don't understand how to use "file:///android_asset". Can you put this in the form of "File file = new File(...);"?
Get the assetmanager, get the assetfiledescriptor , get the filedescriptor from assetfiledescriptor.
But then how do you convert a FileDescriptor to a File?
David - did you get an answer to this? if yes, please post. ty.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.