Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android将xml文件从资产复制到内部内存_Android_Xml - Fatal编程技术网

Android将xml文件从资产复制到内部内存

Android将xml文件从资产复制到内部内存,android,xml,Android,Xml,嗨,我有一个xml文件,其中包含我将用于填充我的应用程序的数据。我需要能够读/写这个文件,我认为这是不可能的,因为它成为资产文件夹中的静态资源。启动时是否有办法将此文件复制到我可以这样使用它的位置?或者,从本地资源读写xml的最佳方式是什么 试试这个 是,您不能在运行时在资产文件夹中写入文件。欲知详情 您可以使用下面的代码从资产复制到SD卡,然后编写它 AssetManager assetManager = this.getAssets(); InputSt

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

试试这个

是,您不能在运行时在资产文件夹中写入文件。欲知详情

您可以使用下面的代码从资产复制到SD卡,然后编写它

    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"/>