I'm trying to create a text file in a folder where the user can see for the file explorer apps of the mobile device, preferably in Documents folder.
Here is what I've till now (consider that we have all required permissions)
final directory = Platform.isAndroid
? await getExternalStorageDirectory()
: await getApplicationDocumentsDirectory();
final filePath = '${directory!.path}/example.txt';
File file = File(filePath);
await file.create();
await file.writeAsString('Some random text content');
After that I try to find this new file on my file explorer apps, but it seems to not exists and no error logs can been seen.
If you want see an example code I wrote this PoC -> https://github.com/felipeemidio/save-local-file-poc
What I've tried so far?
- If you try to execute
file.exists()
command it'll returntrue
. - Use
image_gallery_saver
lib to "download" my file results on an error for trying to save aplain/text
file where should only beimage/*
files. - Use
getExternalStorageDirectory(type: StorageDirectory.documents)
instead ofgetExternalStorageDirectory()
changes nothing. downloads_path_provider
lib does not support null-safety.- If you set the directory path with the static value
'/storage/emulated/0/Documents/example.txt'
, is possible to see the file through Archives app, but will not show when you select the option to see all Documents files.
CodePudding user response:
Try file path as
String filePath = '/storage/emulated/0/Downloads/example.text';
and don't forget to add permissions in manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />