chubbsondubs
chubbsondubs

Reputation: 38676

Storing Files on Android that don't get deleted on update or uninstall

I want to store some data to a File (not database) on the Android device. Currently I'm using Context.getFileDir() as the parent directory. However, when you update the app that directory gets deleted. Is there a directory I can use from Context that won't get wacked when the user goes to update the application? Does it have to be the SD Card since that's not always generally available on all phones?

Upvotes: 1

Views: 1499

Answers (2)

chikka.anddev
chikka.anddev

Reputation: 9629

If you want to file not to be deleted on uninstall is not possible as you might know that uninstall will delete all the data.

While if you want to save the file on update of code you can use that same method as you are using by creating file on getFileDir(); just you have to check out each time before creating file that if file already exists or not.

If file exists there is no need to create again and if it is not there then create it.

I am assuming that you have done all stuff of file creating properly. Just add below code before creating it.

if(f.exists()) //Where f is your file
{
    //Don't create the file, it already exists
}
else
{
    //Create the file, since it didn't exist
}

Upvotes: -3

CommonsWare
CommonsWare

Reputation: 1006614

However, when you update the app that directly gets deleted.

No, it doesn't.

Is there a directory I can use from Context that won't get wacked when the user goes to update the application?

No files ever "get wacked when the user goes to update the application".

All files in the on-board flash will "get wacked when the user" uninstalls the app.

Does it have to be the SD Card since that's not always generally available on all phones?

External storage is "generally available" on all Android devices that have the Android Market. It might not be available in specific circumstances (e.g., it is mounted on a host PC, external storage was removable and was actually removed).

Upvotes: 6

Related Questions