19

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.

SteD
  • 13,909
  • 12
  • 65
  • 76
Brendan Maguire
  • 4,188
  • 4
  • 24
  • 28

4 Answers4

31

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
9

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

William Scott
  • 2,109
  • 1
  • 24
  • 21
2

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.

tanius
  • 14,003
  • 3
  • 51
  • 63
1

If you have any asset like PDF file stored inside assets folder then get its path using below line:

/assets/file_name.pdf
Abdul Waheed
  • 562
  • 6
  • 14