简体   繁体   中英

Persist app data and reuse even if app is uninstalled and installed again (using Android 11, API 30)

It looks like Android 11 is designed such that either an app deletes its data when it gets uninstalled or the app stores the data in a remote server.

But my app requirements are different:

My app doesn't store any data on a remote server but it stores user data on user's device itself.

So, my app creates some files in Public Documents directory.

When the app is uninstalled, the data on the device doesn't get deleted.

When the user installs the app again, the app sees that there is previous data in the device and hence it tries to reuse that earlier data.

But, when trying to read the file, Android gives an error/exception that file cannot be read.

I tried setting the file permissions (when it is created for the first time) to anyone can read/write/execute but I don't think that this is working.

So, it looks like the problem is: When the app is installed again, it is a new user in the system and since it is not the owner of the files created by previous installation of the app, Android gives an error/exception.

So, is there any solution?

Or, is it that Android 11 is designed such that either an app deletes its data when it gets uninstalled or the app stores the data in a remote server?

But, when trying to read the file, Android gives an error/exception that file cannot be read.

Your question does not include any source code. My assumption, based on your description, is that you are trying to access the content using filesystem APIs (eg, File ). In that case, this behavior is expected. By default, files in Documents/ are private to an app, and Android considers "an older installation" and "the current installation" of the same application ID to be different apps.

So, is there any solution?

For arbitrary content types, use the Storage Access Framework (eg, ACTION_CREATE_DOCUMENT , ACTION_OPEN_DOCUMENT , ACTION_OPEN_DOCUMENT_TREE ), both for creating the content initially and for accessing it later. Let the user decide where on the user's device (or in the user's cloud storage) that your app should place the user's content. Content created this way also survives app installation, and the user can direct your new app installation to work with the content from your old app installation.

Why can we not just delete all the data once the app in uninstalled? If I decide to re-install later, I can use the same data as before to log on. Right now it gives an error message "you cannot use the same account already on file". I don't know the rationale behind this algorithm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM