Android assets - FileNotFound

heisenberg picture heisenberg · May 8, 2014 · Viewed 11.7k times · Source

I am inside a fragment in this class:

public class NetworksList extends Fragment{

Also inside my onCreate function I'have this piece of code:

        XmlPullParserFactory pullParserFactory;
        try {
            pullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = pullParserFactory.newPullParser();

            InputStream in_s = getActivity().getApplicationContext().getAssets().open("temp.xml");
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in_s, null);
            Toast.makeText(getActivity().getApplicationContext(), "size: ", Toast.LENGTH_LONG).show();
            parseXML(parser);

        } catch (XmlPullParserException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Wish I am trying to use to open XML Files. I have my XML file in the assets folder, but I am getting:

05-08 18:03:11.034  24645-24645/pt.smartgeo.aees W/System.err﹕ java.io.FileNotFoundException: temp.xml
05-08 18:03:11.034  24645-24645/pt.smartgeo.aees W/System.err﹕ at android.content.res.AssetManager.openAsset(Native Method)
05-08 18:03:11.034  24645-24645/pt.smartgeo.aees W/System.err﹕ at android.content.res.AssetManager.open(AssetManager.java:316)
05-08 18:03:11.034  24645-24645/pt.smartgeo.aees W/System.err﹕ at android.content.res.AssetManager.open(AssetManager.java:290)
05-08 18:03:11.034  24645-24645/pt.smartgeo.aees W/System.err﹕ at pt.smartgeo.aees.NetworksList$2.onClick(NetworksList.java:77)

FileNotFound... How can I know where to put my temp.xml file so I can open it in my NetworksList Class?

Answer

Jorgesys picture Jorgesys · May 8, 2014

if you are sure to have a file temp.xml inside /assets folder, (must be at the same level of /src and /res inside your project), just try a refresh, F5.

the way that you are loading the file from assets is correct:

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