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.

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.