-1

I want to include a file (which contains key-value-pairs) in an android app. I need to have write-access to the file, so I can not put it in raw or assets.

How can I do this? In standard java, I would just have put it in the project root.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
AHahn94
  • 25
  • 1
  • 9
  • 1
    Include the file in `raw` or `assets`. Then copy it on the storage, *if it's not already there*. Then use the file from the storage. DONE! It's nearly a duplicate of [Shipping an app with a prepopulated database](http://stackoverflow.com/questions/2409126/android-pre-populated-database). – Phantômaxx Dec 23 '17 at 21:56

1 Answers1

0

You would want to read and write to the internal storage of the device. getFilesDir() gives you the file object for your app's private internal storage. And as per the note in the documentation here, if you use MODE_PRIVATE with openFileInput or openFileOutput, the file will be private to your app only.

Kartik Arora
  • 571
  • 4
  • 10
  • Thanks. But how do I include the file with the default values when building the app? – AHahn94 Dec 23 '17 at 21:27
  • Keep it in `raw`, copy content from the file in `raw` to file in internal storage. If this works for you, I'll update the answer. – Kartik Arora Dec 23 '17 at 21:29
  • Well, to me this looks like bad design on androids part. I do not see a problem in just adding a folder to the apk that will automatically extract to the internal storage, thus not making me do that manually. But I guess I will have to deal with it the way it is. Thank you for your answer. – AHahn94 Dec 24 '17 at 08:42
  • You can alternatively download the file from a web server on app's first run and store it in the internal storage. – Kartik Arora Dec 24 '17 at 11:24