10

I would like to know if there is a way to store a small amount of data permanently.
By permanently I mean I want the data to persist even if user clears app data / uninstalls app.
I know that shared preferences and databases are deleted when user clears app data / uninstalls app.

I also know that I can save stuff on SD Card, but what if device does not have SD Card / SD Card is unmounted?

I think that the best option would be to save the data on device internal memory, but is it possible to do that without getting the data deleted when clears app data / uninstalls app?

pandre
  • 6,685
  • 7
  • 42
  • 50

2 Answers2

16

You shouldn't do that, you shouldn't force the users to keep data on their phones without their consent.

Anyway, application data directory will be deleted after uninstall, but NOT after an update

The only way to have persistent data is to use the SD card, but again, users won't like to have the data on their card after the app is uninstalled

Or you can consider:

  1. Storing the data on a remote server with some kind of authentication to retrieve it
  2. Using Data Backup service
BFil
  • 12,966
  • 3
  • 44
  • 48
  • is there any other solution which I can implement in my sdk without forcing my users to make any changes in Manifest unlike the "Data Backup" option is forcing me too – therealprashant Nov 05 '15 at 13:05
  • The *real* question is what do you need to achieve with persistent storage. If you are the owner of the device *and* have physical access to it *AND* the device is rooted or at least `/system` partition can be somehow made writable, you can store needed files there. You can also have a persistent (always installed / not uninstallable via GUI) app this way by putting it inside `/system/app` directory. – Lukasz032 Feb 24 '20 at 23:59
2

Environment.getDataDirectory() is at least one directory where you can save files. It's on the internal storage (/data).

One case I use it is to define whether a service is running; I create a .lock file in there and always check whether it exists or not.

Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
  • Don't you need root for that? – neteinstein May 24 '11 at 10:49
  • Hi, I am trying to create a file in Environment.getDataDirectory(), but I get java.io.IOException: Permission denied. I checked and it seems that this directory is not writable: http://stackoverflow.com/questions/1998400/data-directory-have-no-read-write-permission-in-android – pandre May 24 '11 at 10:54
  • I'm doing the same thing on my android 2.2 device and it works _without_ root privileges. – Ilya Saunkin May 24 '11 at 11:31