【发布时间】:2014-03-22 10:12:17
【问题描述】:
您好,我有一个 xml 文件,其中包含我将用于填充我的应用程序的数据。我需要能够读/写这个文件,我认为这是不可能的,因为它成为资产文件夹中的静态资源。有没有办法在启动时将此文件复制到我可以以这种方式使用它的位置?或者从本地资源读取和写入 xml 的最佳方法是什么?
【问题讨论】:
您好,我有一个 xml 文件,其中包含我将用于填充我的应用程序的数据。我需要能够读/写这个文件,我认为这是不可能的,因为它成为资产文件夹中的静态资源。有没有办法在启动时将此文件复制到我可以以这种方式使用它的位置?或者从本地资源读取和写入 xml 的最佳方法是什么?
【问题讨论】:
试试这个..
是的,您不能在运行时在资产文件夹中写入文件。信息link1,link2
您可以使用以下代码从 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"/>
【讨论】: