1

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?

5
  • Is that the exact error message? If so, is there a reason why you're using a relative path instead of an absolute one? Using a relative path means that there's an opportunity after calling oldBackup.exists() but before its Future completes for your process's current working directory to change, which could cause oldBackup.deleteSync() to fail. Does using oldBackup.existsSync() make any difference? Commented May 5, 2024 at 16:20
  • Not yet, I am busy with some important things, I will give it a try this weekend. Thanks for the reply. Commented May 8, 2024 at 17:29
  • @jamesdlin existsSync is not making any difference Commented May 11, 2024 at 4:56
  • Did you solved this problem yet ? Commented Jun 3, 2024 at 10:34
  • Not yet, I think I have to store it on remote db. Commented Jun 4, 2024 at 11:24

1 Answer 1

0

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.


Solution :

1. Internal Storage :

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.

2. Assets :

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.

3. SharedPreferences:

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Bob for the reply but I want that file to persist in the device even after app uninstallation. That's why I want to store it outside the app's scoped storage. I can't store it in the app's Assets folder because the user will create data inside that cs.db file. I also can't use SharedPreferences because the data inside it gets deleted after app uninstallation. So I am looking for a solution where I can persist that file in the device and can reaccess it.
In that case create new folder in public directory and write files there. It will be only accessible by your package name. Another approach is to upload file to your server and use API's to do modifications.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.