Reputation: 5811
The user of my Android app developed in Flutter should be able to save (export) some data to a text file. The user should be able to find and access this file on his Android device using other apps (including File Managers).
I'm trying:
final directory = await getExternalStorageDirectory();
final file = File('${directory.path}/test1.txt');
await file.writeAsString('Test');
On an Android 9 phone this saves the file in:
/storage/emulated/0/Android/data/com.mydomain.myapp/files
However, I can't access the file (or the directory) using a File Manager app (e.g. Files) on the phone.
(If I connect the phone to a Windows PC using a USB cable, I can access the directory, see the test1.txt
file, but I can't view or access it.)
Can anyone point me in the right direction?
Upvotes: 5
Views: 3408
Reputation: 456
I had a similar use-case except I wanted to save "Downloads" folder in Android. Here's what worked for me:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<!-- Need this permission so that other apps see the saved files -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
static Future<void> save(String filename, String content) async {
final directory = await DownloadsPathProvider.downloadsDirectory;
final path = '${directory.path}/$filename';
final file = File(path);
Logger().d("Saving $path");
await file.writeAsString(content);
}
Save the file? I was able to view the file saved using a File Browser without being rooted or anything.
Hopefully that works
Upvotes: 2