0

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.

Gabi Radu
  • 1,107
  • 2
  • 16
  • 35
  • you should put your xml into the res/raw/ folder. here's the link http://developer.android.com/guide/topics/resources/providing-resources.html – Sergey Benner Jan 31 '12 at 09:47
  • 1
    I think this post helpful to you http://stackoverflow.com/questions/4789325/android-path-to-asset-txt-file – Newts Jan 31 '12 at 09:51

2 Answers2

2

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.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1

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

Context context = ...;
AssetManager assManager = context.getAssets();
InputStream is = assManager.open("test.xml");
Jens
  • 16,853
  • 4
  • 55
  • 52