-1

I guess I'm a little confused as to how files are stored on an actual machine (or emulator even).

While programming, I can save my xml file in the assets folder manually, but how to write an app that will have to connect to the network and download the file,save it somewhere and then manipulate it ? where will it store said file ?

I want to create a new file, but I read on another post that the assets folder as such is not available once packaged; So where are they created and stored ? How can they be transferred. Its just, I'm new to this platform and the file system is a little confusing.

user1349663
  • 595
  • 1
  • 7
  • 21

3 Answers3

0

If you want to use XML that is updated, you should think of copying the file(s) from assets to device storage. You can take a look at How to copy files from 'assets' folder to sdcard? to know how this can be done.

Another alternative is to use the database where you can store the parsed data from the XML. So that you need not parse the file whenever you need to access the contents.

Community
  • 1
  • 1
Rajesh
  • 15,724
  • 7
  • 46
  • 95
0

You have two options: call getFilesDir() from your activity to obtain a path to the internal data folder that can only be read/write from your app.

Or, you can write/read your xml file to external storage (SD Card). Use the method Environment.getExternalStorageDirectory() to get the root path of the external storage, then create your own folder as you see fit.

Note that if you write to external storage, every app in the phone will have access to it.

azgolfer
  • 15,087
  • 4
  • 49
  • 46
0

Even I faced this issue. Now I have a xml file which is has application properties.This is packaged in the assets folder.Once packaged we cannot edit a file in assets folder. Now on app load I just copy this file to path returned by

context.getFilesDir().getAbsolutePath();

And the application edit it from the same place. You can see if the file is modified in the FileExplorer panel of DDMS view. The file is stored in the folder named same as your application package name for eg: com.abhi.maps

Alternatively you can also copy it to SD card.However it is risky because, sd card may bot be available all the time.

You can use the following code to copy file from assets folder:

    private static void copyFile(String filename, Context context) {
    AssetManager assetManager = context.getAssets();

    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open(filename);
        String newFileName = context.getFilesDir() + "/" + filename;
        out = new FileOutputStream(newFileName);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }

}

Hope it helps! :)

Abhilasha
  • 929
  • 1
  • 17
  • 37