I Z I Z - 1 year ago 95
Android Question

Android: how do I create File object from asset file?

I have a text file in the assets folder that I need to turn into a File object (not into InputStream). When I tried this, I got "no such file" exception:

String path = "file:///android_asset/datafile.txt";
URL url = new URL(path);
File file = new File(url.toURI()); // Get exception here

Can I modify this to get it to work?

By the way, I sort of tried to "code by example" looking at the following piece of code elsewhere in my project that references an HTML file in the assets folder

public static Dialog doDialog(final Context context) {
WebView wv = new WebView(context);

I do admit that I don't fully understand the above mechanism so it's possible that what I am trying to do can't work.


Answer Source

You cannot get a File object directly from an asset, because the asset is not stored as a file. You will need to copy the asset to a file, then get a File object on your copy.