Home > OS >  Android: Get path to and file listing of assets directory?
Android: Get path to and file listing of assets directory?

Time:01-01

I want to add a bunch of HTML files to my assets directory and determine at runtime how many there are and what their paths (or URLs) are.

How can I get a list of files in my assets directory?

How can I form path strings or URLs to read each one individually?

CodePudding user response:

How can I get a list of files in my assets directory?

Call list() on an AssetManager to get a list of assets packaged in your app. You can get an AssetManager by calling getAssets() on a Context.

Note that these are not files on the device. They are files on your development machine; they are merely assets in the app.

How can I form path strings or URLs to read each one individually?

That depends on what you mean by "read each one".

If you want to read the HTML in manually, call open() on the AssetManager, passing in the relative path to the asset. IOW, for a file on your development machine in app/src/main/assets/hi/there.html, you would call open("hi/there.html")).

If you want to display the HTML in a WebView, use file:///android_asset/hi/there.html with loadUrl() on the WebView, for an asset located at the relative path hi/there.html.

  • Related