Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the path to the Android assets folder in the application package

Tags:

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.

like image 959
charles young Avatar asked May 02 '11 18:05

charles young


People also ask

Where is the Android assets folder?

Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder.

How do I get to my assets folder?

File > New > folder > assets Folder Two ways: Select app/main folder, Right click and select New => Folder => Asset Folder.

What method can be used to access a file in the asset's directory of your application?

Place your text file in the /assets directory under the Android project and use AssetManager class as follows to access it.


1 Answers

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);
like image 120
duggu Avatar answered Oct 12 '22 05:10

duggu