Gabi Radu
Gabi Radu

Reputation: 1107

Using filePath for an xml from assets folder in Android

I am trying to use the filepath of an xml file that exists in the assets folder of android, like this:

String filename = "file:///android_asset/test.xml";
FileInputStream fis =  new FileInputStream(new File(filename));

System.out.println("FileInputStream : " + fis); // here it fails I get an

java.io.FileNotFoundException: /file:/android_asset/text.xml

error and it does not make sense to me.

I made an apk and opened it then with total commander and the xml is in the assets folder. Do you see a problem in my approach? Thank you.

Upvotes: 0

Views: 5261

Answers (2)

Lalit Poptani
Lalit Poptani

Reputation: 67286

Just get the InputStream,

InputStream is = getApplicationContext().getAssets().open("test.xml");

You can also keep your xml in the res/xml folder and just get it working by

getResources().getXml(R.xml.test);

Here is a complete example for the same.

Upvotes: 2

Jens
Jens

Reputation: 17077

Assets are accessed using the AssetManager, in your case - like this:

Context context = ...;
AssetManager assManager = context.getAssets();
InputStream is = assManager.open("test.xml");

Upvotes: 1

Related Questions