【问题标题】:Android copying xml file from assets to internal memoryAndroid将xml文件从资产复制到内部存储器
【发布时间】:2014-03-22 10:12:17
【问题描述】:

您好,我有一个 xml 文件,其中包含我将用于填充我的应用程序的数据。我需要能够读/写这个文件,我认为这是不可能的,因为它成为资产文件夹中的静态资源。有没有办法在启动时将此文件复制到我可以以这种方式使用它的位置?或者从本地资源读取和写入 xml 的最佳方法是什么?

【问题讨论】:

    标签: android xml


    【解决方案1】:

    试试这个..

    是的,您不能在运行时在资产文件夹中写入文件。信息link1link2

    您可以使用以下代码从 assets 复制到 sdcard 然后您可以编写它。

        AssetManager assetManager = this.getAssets();           
        InputStream in = assetManager.open("yourxmlfile.xml");
        File SDCardRoot = new File(Environment.getExternalStorageDirectory().toString()+"/Folder");   
        SDCardRoot.mkdirs(); 
        File file = new File(SDCardRoot,"hello.xml");
        FileOutputStream fileOutput = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while((bufferLength = in.read(buffer)) > 0)
        {
               fileOutput.write(buffer, 0, bufferLength);
        }
        fileOutput.close();
    

    别忘了在清单中添加读/写权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 2021-12-24
      • 2019-09-16
      • 2023-03-05
      相关资源
      最近更新 更多