【问题标题】:Read xml file from Android Asset folder从 Android Asset 文件夹中读取 xml 文件
【发布时间】:2012-05-11 11:38:36
【问题描述】:

我正在尝试从 assets 文件夹中读取相同的文件“xmlfile.xml”,并从 SD 卡 sdcard/download/ 中读取另一个副本

我可以从 SD 卡读取:

  • unfile 返回
  • 网站返回

我无法从 Assets 文件夹中读取:

  • unfile 返回 False
  • 网站返回

这段代码不工作

        File source = new File("file:///android_asset/xmlfile.xml");
        boolean unfile = source.isFile();
        boolean Esiste = source.exists();

        try
        {
          // todo
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

这段代码可以正常工作

        File source = new File("/sdcard/" + "download" + "/" + "xmlfile.xml");
        boolean unfile = source.isFile();
        boolean Esiste = source.exists();

        try
        {
          // todo
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

谁能解释我如何从 Assets 文件夹中读取文件。

谢谢 马可

【问题讨论】:

标签: android xml android-assets


【解决方案1】:

要打开资产,您需要以下代码:

InputStream is = getAssets().open("xmlfile.xml")

【讨论】:

    【解决方案2】:

    使用此功能

    // Converting given XML file name to String form
    String mOutputText = getxml("yourxml.xml");
    
    /**
     * Function used to fetch an XML file from assets folder
     * @param fileName - XML file name to convert it to String
     * @return - return XML in String form
     */
    private String getXml(String fileName) {
        String xmlString = null;
        AssetManager am = context.getAssets();
        try {
            InputStream is = am.open(fileName);
            int length = is.available();
            byte[] data = new byte[length];
            is.read(data);
            xmlString = new String(data);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return xmlString;
    }
    

    【讨论】:

    • 对于文件,我的意思是类link
    • 替换 InputStream is = am.open(path);与 InputStream is = new FileInputStream(File) 我认为它可能会有所帮助
    【解决方案3】:

    这肯定会对你有所帮助。 要读取文件表单资产文件夹,您需要一个 InputStream 对象。

    语法:

    InputStream yourobj=getApplicationContext().getAssets().open("Path to xml file");
    

    所以实时代码可以是

    InputStream in_s = getApplicationContext().getAssets().open("www/application/app/client/controllers/data1.xml");
    

    这里data1.xml是路径里面的文件。

    【讨论】:

      【解决方案4】:
          try 
          {
              AssetManager aManager = Class.getAssets();
              InputStream iStream = aManager.open("file.xml");
              int length = iStream.available();
              byte[] data = new byte[length];
              iStream.read(data);
              assetString = new String(data).toString();
          }
          catch (IOException e)
          {
              e.printStackTrace();
          }
      

      【讨论】:

        【解决方案5】:

        我发现执行以下操作更容易:

        val xmlStream = context.resources.getXml(R.xml.example)
        

        我相信这是 XmlPullParser 的一个子集,但它运行良好。

        【讨论】:

          猜你喜欢
          • 2020-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-19
          • 2014-06-22
          相关资源
          最近更新 更多