0

Hi i have an xml file that contains the data I'm going to use to populate my app. i need to be able to read/write this file which i believe is not possible as it becomes a static resource in the assets folder. Is there a way on launch to copy this file to a location where i can use it in this way? or what is the best way to read and write a xml from a local resource?

Luke Batley
  • 2,384
  • 10
  • 44
  • 76

1 Answers1

0

Try this..

Yes you can not write file in the assets folder at run time. For info link1, link2

You can use below code copy from assets to sdcard then you can write it.

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

And also don't forget to add read/write permission in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Community
  • 1
  • 1
Hariharan
  • 24,741
  • 6
  • 50
  • 54