Reputation: 53
Using a flutter app, I created a db file in 'storage/emulated/0/Documents/Keep/cs.db' path. I deleted the app. I again installed the app and want to delete/copy the file I created. But now I can't do anything to that file. Why?
I have the required permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I have added android:requestLegacyExternalStorage="true" as well.
I am doing the following thing to delete that file:
File oldBackup = File(destinationPath);
print(await oldBackup.exists());
oldBackup.deleteSync();
print(await File(destinationPath).exists());
await oldBackuo.exists() returns true,
but oldBackup.deleteSync() and oldBackup.delete() returns "PathNotFoundException: Cannot delete file, path = 'storage/emulated/0/Documents/Keep/cs.db' (OS Error: No such file or directory, errno = 2)"
How? How can I delete/copy that file when I 2nd time install the app?
Upvotes: 1
Views: 117
Reputation: 2868
Its Android security feature, that isolates app from access to the specific part of the phone's memory. By doing this, system ensure that each app can process or use its own database and increases the app's security. By default your app can only its out internal storage folder and not chance to access any external storage source like this
/storage/emulated/0/Documents
Android 11 doesn't allow to access directly files from storage you must have that file into your app package(something like : com.example.yourpackage) cache.
Save the data file, for example, cs.db, to your app's internal storage directory just as app installation or user action. This keeps the app in persistence state. However, it is deleted upon app un-installation.
Instead, cs.db file may be located within your project's asset folder.
By means of Activity class open the file from the assets folder of your project and immediately read the data that is stored in it. However, this might be best with static data that cannot be modified.
Consider this if db is static.
SharedPreferences is a light data storage facility presented by the Android platform. Not only it is great for keeping small amount of key-value pairs that you should be able to use repeatedly within app sessions or even after device reboots but also it will ensure that all your data is secure and constant.
Make sure to select a strategy that will agree with the app's data specifics and usage styles.
Links for more information :
https://developer.android.com/about/versions/11/privacy/storage
https://developer.android.com/training/data-storage/manage-all-files#all-files-access-google-play
https://developer.android.com/training/data-storage/shared-preferences
https://developer.android.com/training/data-storage
Upvotes: 0