0

I just want the path of the assets directory which I want to use as a variable throughout my app.

I know we can refer a file in the "assets" directory using AssetManager, but I don't want to refer a file as of now, I just want the path of the assets directory.

Can anybody help me with this?

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
  • Are you using this to form content Uris? What specifically is your use case? – David Snabel-Caunt Jul 13 '11 at 12:52
  • @David: Since android has a file size restriction on files inside `raw` directory, I will be placing my files in `assets` directory, and during runtime, I would be retrieving them as need... So basically, the filename would be sort of dynamic, but the path of `assets` directory would remain same... So want to get the path of `assets` directory. – Mahendra Liya Jul 13 '11 at 12:56
  • Understood - I can give you the path in the form of a Uri that you can use with ContentProviders, if that is what you need? Obviously this only works if you use a ContentProvider to load the asset, which is why I ask. – David Snabel-Caunt Jul 13 '11 at 13:09
  • @David: I am not sure if that will work or not, but I can give it a try. Actually, I'll be setting the path for the `assets` directory in one of my custome `Settings` class, which will be used by other API calls... Do you see this is possible... May be you can share some code which you thing may work, and I'll give it a shot... Thanks in advance. – Mahendra Liya Jul 14 '11 at 05:57

2 Answers2

4

I just want the path of the assets directory which I want to use as a variable throughout my app.

There is no "path of the assets directory" on the device.

I know we can refer a file in the "assets" directory using AssetManager, but I don't want to refer a file as of now, I just want the path of the assets directory.

There is no "path of the assets directory" on the device.

Can anybody help me with this?

No, because there is no "path of the assets directory" on the device. Please use AssetManager to retrieve an InputStream to your assets as needed.

Since android has a file size restriction on files inside raw directory, I will be placing my files in assets directory, and during runtime, I would be retrieving them as need

The "restriction" is the same for assets, AFAIK. The usual way to circumvent the "restriction" is to give the file an extension that Android's build tools will not try to compress (.mp3 works IIRC).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have been a great fan of your books. I am actually developing a jar file which will is planned to be used in Android project(s). I am aware that the file size restriction can be overcomed by using extensions for files (like .jpg, .mp3, etc) which aren't compressed by Android... I want something like, the user places a certain file(s) in the assets directory and can use it later through API calls exposed by my jar file.. Is my approach correct? If no, do you think there is an alternative which could be better option? – Mahendra Liya Jul 14 '11 at 04:56
  • "I want something like, the user places a certain file(s) in the assets directory and can use it later through API calls exposed by my jar file.. Is my approach correct? " -- so long as you get a `Context` so you can get an `AssetManager`, that sounds fine. I worry a bit about designs like this that might impose artificial restrictions on the reuser (e.g., you mandating certain file names or directories in `assets/`). "I have been a great fan of your books" -- thanks! – CommonsWare Jul 14 '11 at 10:17
  • @CommansWare: accepting your answer.. Lastly, just want an opinion from you: Do you see any possible way in which I can enable the user(s) of my `jar` file to place files in a certain location, which can be later referenced in their app code to use feature / functionalities provided by my `jar` file? Thanks in advance! – Mahendra Liya Jul 14 '11 at 10:50
  • @mahi: I am afraid that I do not understand that last question. – CommonsWare Jul 14 '11 at 12:29
0

Try using

AssetFileDescriptor

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148