Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android assets - FileNotFound

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?

like image 617
heisenberg Avatar asked May 08 '14 17:05

heisenberg


People also ask

What is asset manager in Android?

android.content.res.AssetManager. Provides access to an application's raw asset files; see Resources for the way most applications will want to retrieve their resource data.

What are Android assets?

Assets provide a way to include arbitrary files like text, xml, fonts, music, and video in your application. If you try to include these files as "resources", Android will process them into its resource system and you will not be able to get the raw data.

Where is assets folder in Android Studio?

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box. Keep all the settings default.


1 Answers

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");
like image 66
Jorgesys Avatar answered Sep 22 '22 06:09

Jorgesys