Cannot find the path provided by getApplicationDocumentsDirectory()

十年热恋 提交于 2021-01-22 03:38:33

问题


I tested this flutter app example: https://github.com/tensor-programming/flutter_read_write_file_and_path

But, browsing my device with a file-manager (Solid Explorer), I cannot find the file "db.txt" created by this example.

final path = await localPath;

I checked in many folders including:

  • /storage/emulated/0 (= sdcard)
  • sdcard/Android/data
  • sdcard/data

Where is this file? Should I root my device to find/see it?

What string to add after the "path" variable to reach the Download folder? Or, how to get the absolute path to the Download folder?

Device: GALAXY S8+ SM-G955F. Android 8.0. Not Rooted. Flutter beta v0.5.1. Dart 2.0.0-dev.58.0. Windows 10. Thanks and regards


回答1:


If you're using a file explorer app installed on the device, then I don't believe you'll be able to access the files stored for a different app in it's Application Documents directory. This is part of the security model that isolates apps from each other.

If you have used Android Studio to build and deploy the app to your device, then you can got to View > Tool Windows > Device File Explorer with your device attached to your PC via USB to access these files.

I find for my own Flutter apps that the relevant path is /data/usr/0/…/app_flutter for the example you gave, this would be /data/usr/0/com.example.localstorageexample/app_flutter

(If you haven't used Android Studio to deploy the demo you reference, how did you get it on your device?)




回答2:


Use: getExternalStorageDirectory(); And find storage directory instead. /storage




回答3:


You can do this

final Directory extDir = await getExternalStorageDirectory();
final String dirPath = extDir.path.toString().substring(0,20);
await Directory(dirPath).create(recursive: true);
final String filePath = '$dirPath/';

You might need to edit the parameters on the second line according to your platform for Android. This will create your file, and make it accessible to your users.




回答4:


You can just take a look at the source file of the code.
In this case, only the documentation (/// marked comments) provides any useful information, which is the following:

  • For iOS it is NSDocumentDirectory.

  • On Android, which is what you asked for, the directory can be found as /data/data/<package_name> and not as /data/<package_name>.

I did not take a look into the project's code, which means that it might be possible that your file is not stored in getApplicationDocumentsDirectory().



来源:https://stackoverflow.com/questions/51423058/cannot-find-the-path-provided-by-getapplicationdocumentsdirectory

标签