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

。_饼干妹妹 提交于 2019-12-23 02:55:18

问题


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?


回答1:


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
}



回答2:


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).



来源:https://stackoverflow.com/questions/4834338/storing-files-on-android-that-dont-get-deleted-on-update-or-uninstall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!