Caner
Caner

Reputation: 59168

Application config file

My application needs a configuration file. In the order of priority:
1) User should be able to update it by connecting the phone to the pc & select mass storage mode
2) My application needs to be able to read & overwrite
3) I should be able to distribute it with my installer

How can I do this? I checked "Using the External Storage" on this this page but getExternalFilesDir(null).getAbsolutePath() returns /mnt/sdcard/Android/data/com.MyApp/files and I think I cannot put files in that location with installer.

Upvotes: 2

Views: 6322

Answers (3)

inazaruk
inazaruk

Reputation: 74780

According to requirement #1 - external storage is the only place. But you're right about #3 - your installer can not write files at that location.

You can resolve this by creating custom Application file (see Application and <application android:name="...">). In your class that extends Application in onCreate method check whether configuration file exists, and if not - create default one by copying it from application resources/assets.

Upvotes: 0

Mojo Risin
Mojo Risin

Reputation: 8142

First you can't use the external storage ( sd card ) because it is mounted when connected to PC so you won't have access to it. If you want to have access to the config file while connected to pc you have to store it in PhoneStorage. Here is the documentation for the internal storage. In order to distribute your config file with the installer you can use pre-defined config file in the Assets folder and copy it in the InternalStorage at install. So these are the basic steps

  1. Place pre-defined config file in the assets folder
  2. Copy the config file from assets folder to internal storage on install (there is no way to handle your application install event but simple can check if the destination file exists and if not copy from assets)
  3. Work with your config file placed in the internal storage

If you have some issues implementing them please feel free to ask.

Upvotes: 3

Fredrik Leijon
Fredrik Leijon

Reputation: 2802

Write it to sdcard/, let your app have permissions to read/write on the sdcard. If the file is missing assume that your app is being run for the first time and write the config file to the sdcard (you could include a template as a resource in your apk)

Upvotes: 0

Related Questions