【问题标题】:Android assets - FileNotFoundAndroid 资产 - FileNotFound
【发布时间】:2014-06-26 05:34:15
【问题描述】:

我在这个类的一个片段中:

public class NetworksList extends Fragment{

在我的onCreate 函数中,我也有这段代码:

        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();
        }

希望我尝试用它来打开 XML 文件。我的资产文件夹中有我的 XML 文件,但我得到了:

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... 我如何知道将 temp.xml 文件放在何处以便在 NetworksList 类中打开它?

【问题讨论】:

  • 看起来应该可以了。您可以将您的 xml 放在 raw 文件夹中并通过它的 id 访问它:context.getResources().openRawResource(R.raw.temp);
  • 原始文件夹在哪里?
  • 可以在res文件夹下创建。

标签: android xml android-assets


【解决方案1】:

如果您确定在/assets 文件夹内有一个文件temp.xml,(必须与您的项目内的/src/res 处于同一级别),只需尝试刷新,F5.

您从assets 加载文件的方式是正确的:

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    我的意思是消除“.xml”部分。我为同样的错误尝试了这个,它对我有用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多