Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to find the absolute path of the assets folder?

I need to get the absolute path of the assets folder in my app as I want to serve the files within using a webserver and it needs the absolute path. Is this possible?

I would have thought there might be something like this:

String rootDir = getAssets().getRootDirectory();

but there's not.

Any help appreciated, cheers.

like image 519
Brendan Maguire Avatar asked Feb 17 '11 14:02

Brendan Maguire


People also ask

Where is the Assets folder in Android?

In Android Studio, click on the app folder, then the src folder, and then the main folder. Inside the main folder you can add the assets folder.

How do I access assets files on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 – Right click app >> New >> Folder >> Assets folder.

How do I find the assets folder?

Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box.


3 Answers

Is this possible?

No. There is no "absolute path of the assets folder in [your] app". Assets are stored in the APK file.

In select cases, such as URLs supplied to a WebView, you can use the special file:///android_asset base URL to reference files in your assets.

like image 173
CommonsWare Avatar answered Oct 11 '22 19:10

CommonsWare


You can always copy files from the assets directory in the APK to a folder on the device, then serve that folder.

like image 28
William Scott Avatar answered Oct 11 '22 20:10

William Scott


As mentioned, Android assets cannot be accessed with absolute paths in the device file system. So whenever you have to provide a filesystem path to a method, you're out of luck.

However, in your case there are additional options:

I want to serve the files within using a webserver and it needs the absolute path.

Needing the absolute path is only true if you want to serve the file as a static file with the default mechanism a webserver provides for that. But webservers are much more flexible: you an map any path in an URL to any data source you can access: files, databases, web resources, Android resources, Android assets. How to do that depends on the web server you use.

For example, you can define for youself that any URL starting with https://example.com/assets/ should be mapped to the assets folder of your Android APK. You can then open the asset as an InputStream and serve the content to the webserver's client.

like image 43
tanius Avatar answered Oct 11 '22 20:10

tanius