79

I am working on some application where I have to update some files present in assets / raw folder runtime from some http location.

Can anyone help me to by sharing how to write files in assets or raw folder?

dwarkesh
  • 1,378
  • 2
  • 10
  • 12

5 Answers5

149

It cannot be done. It is impossible.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
  • 5
    Actually, I've done it with a view glitch (a webview showed my my java source code and then the html file it loaded from assets contained my java source code without spaces or anything). I am currently looking around for how to recreate it. – CQM Sep 08 '11 at 17:08
16

Why not update the files on the local file system instead? You can read/write files into your applications sandboxed area.

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Other alternatives you may want to look into are Shared Perferences and using Cache Files (all described at the link above)

jing
  • 226
  • 2
  • 5
14

You cannot write data's to asset/Raw folder, since it is packed(.apk) and not expandable in size.

If your application need to download dependency files from server, you can go for APK Expansion Files provided by android (http://developer.android.com/guide/market/expansion-files.html).

Suryavel TR
  • 3,576
  • 1
  • 22
  • 25
1

Another approach for same issue may help you Read and write file in private context of application

                 String NOTE = "note.txt";  
                 private void writeToFile() {
        try {
         OutputStreamWriter out = new OutputStreamWriter(openFileOutput(
                NOTES, 0));

         out.write("testing");
         out.close();
         }

        catch (Throwable t) {
        Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
        }
             }


           private void ReadFromFile()
      {
        try {
        InputStream in = openFileInput(NOTES);
        if (in != null) {
            InputStreamReader tmp = new InputStreamReader(in);
            BufferedReader reader = new BufferedReader(tmp);
            String str;
            StringBuffer buf = new StringBuffer();
            while ((str = reader.readLine()) != null) {
                buf.append(str + "\n");
            }
            in.close();
            String temp = "Not Working";
            temp = buf.toString();
            Toast.makeText(this, temp, Toast.LENGTH_SHORT).show();
        }
    } catch (java.io.FileNotFoundException e) {
        // that's OK, we probably haven't created it yet
    } catch (Throwable t) {
        Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
    }
}
Vikrant
  • 1,102
  • 8
  • 5
1

You Can't write JSON file while in assets. as already described assets are read-only. But you can copy assets (json file/anything else in assets ) to local storage of mobile and then edit(write/read) from local storage. More storage options like shared Preference(for small data) and sqlite database(for large data) are available.