Antilope
Antilope

Reputation: 445

access the file system

I put my files in the directory assets. how do I get them? I tried following the instructions but have not worked:

File f = new File("/data/data/ant.BrowserX/files/" + pagCorrente + ".html");

File f = new File("file:///android_asset/" + pagCorrente + ".html");

there are no errors but running I can not access the file. How do I access the directory "assets"?

EDIT

thanks for the trick. I have the problem that I use a BufferedReader to read the file, and initialize it so, and not with an InputStream.

in = new BufferedReader(new FileReader(f));

how could I do?

Upvotes: 0

Views: 1272

Answers (3)

Barak
Barak

Reputation: 16393

Simple answer, you don't access the "directory" assets. There is no directory structure in an .apk, it's just data. The framework knows how to access it (getAssets()), but you can't use it like a standard directory.

That being said, if you are trying to use a WebView and access a html file (which is what it looks like) they put a "fake" path reference in that you can use like this:

WebView wv = (WebView)this.findViewById(R.id.yourWebView); 
wv.loadUrl("file:///android_asset/" + pagCorrente + ".html");

Upvotes: 1

Luciano
Luciano

Reputation: 2796

I know you can use this to get a inputstream :

  final AssetManager assetMgr = this.getResources().getAssets();  
  InputStream is = assetMgr.open(pagCorrente +".html"); 

So probably you can use:

   File f = new File(this.getResources().getAssets() + pagCorrente + ".html");

Upvotes: 2

Cruceo
Cruceo

Reputation: 6834

You can get an InputStream from the file through the getAssets() method inside getResources():

 InputStream is = getResources().getAssets().open("file.html");

Then just convert the stream to whatever you need

Upvotes: 4

Related Questions