Android assets - FileNotFound

I am inside a fragment in this class:

public class NetworksList extends Fragment{

Also inside my function onCreate, 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();
        }

A wish that I am trying to use to open XML files. I have an XML file in the resource folder, but I get:

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 do I know where to place the temp.xml file so that I can open it in my NetworkList class?

+4
source share
2 answers

If you are sure that you have a file temp.xmlinside /assets, (it should be at the level /srcand /resinside your project), just try updating F5.

the way to download the file from is assetscorrect:

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

:

InputStream in_s = getActivity().getApplicationContext().getAssets().open("temp");

.xml. , !

0

Source: https://habr.com/ru/post/1539802/


All Articles