skip to Main Content

Imagine I have created a File (import 'dart:io).
I need the user to download the file (to their Download or Documents directory).

This is how I create the file. getApplicationDocumentsDirectory() is from permission_handler.

final dir = await getApplicationDocumentsDirectory();
    final file = File(
        '${dir.path}/myFile.svg');
    await file.writeAsString(content);

The file path looks like this on Android:

/data/user/0/com.example.www/app_flutter/myFile.svg

Apparently this is a path that a normal user wouldn’t be able to open.

What I tried so far:

  • Opening the local path in a browser so the user can pick a download location themselves (using url_launcher)
  • using the open_file package to open the file

Edit:

Using the open_file_plus package like this:

await OpenFile.open(path);

actually allows me to pick an app to open the file (for example the Chrome browser).
But from there the user is still stuck, as no app allows you to "re-download` the file.

2

Answers


  1. Public Android path is: /storage/emulated/0/Download

    My solution use 2 packages, you need to add them to pubspec.yaml:

    path_provider:  ^2.0.14
    permission_handler:  ^10.2.0
    

    You can write an helper like this one:

    import 'dart:io';
    import 'package:path_provider/path_provider.dart';
    import 'package:permission_handler/permission_handler.dart';
      
    // To save the file in the device
    class MyFileStorage {
      static Future<String> getExternalDocumentPath() async {
        // To check whether permission is given for this app or not.
        var status = await Permission.storage.status;
        if (!status.isGranted) {
          // Permission not granted, ask for permission first
          await Permission.storage.request();
        }
        Directory _directory = Directory("");
    
        if (Platform.isAndroid) {
           // Redirects it to download folder in android
          _directory = Directory("/storage/emulated/0/Download");
        } 
      
        final exPath = _directory.path;
        print("Saved Path: $exPath");
        await Directory(exPath).create(recursive: true);
    
        return exPath;
      }
      
      static Future<String> get downloadPath async {
        final String directory = await getExternalDocumentPath();
        return directory;
      }
      
    static Future<File> writeString(String bytes, String name) async {
      final path = await downloadPath;
        // Create a file for the path of
          // device and file name with extension
        File file= File('$path/$name');;
        print("Save file");
          
          // Write the data in the file you have created
        return file.writeAsString(bytes);
      }
    }
    

    And you need to add theses permission to AndroidManifest:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    Then use it like this:

    await MyFileStorage.writeString("lorem ipsum ...", "my-file.txt");
    
    Login or Signup to reply.
  2. import 'dart:io';
    import 'package:path_provider/path_provider.dart';
    import 'package:permission_handler/permission_handler.dart';
    
    // Get the external storage directory path
    Future<String> getExternalStorageDirectoryPath() async {
      var status = await Permission.storage.status;
      if (!status.isGranted) {
        await Permission.storage.request();
      }
      Directory directory = await getExternalStorageDirectory();
      return directory.path;
    }
    
    // Write the file to the external storage directory
    Future<File> writeFileToExternalStorage(String content, String fileName) async {
      String directoryPath = await getExternalStorageDirectoryPath();
      File file = File('$directoryPath/$fileName');
      await file.writeAsString(content);
      return file;
    }
    

    /// you can use the open_file package to open the file with the appropriate app or allow the user to share the file

    import 'package:open_file/open_file.dart';
    
    File file = await writeFileToExternalStorage(content, 'myFile.svg');
    await OpenFile.open(file.path);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search