Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the (absolute) path to the Download folder?

Tags:

flutter

dart

In a Flutter project, how to get the (absolute) path to the Download folder in my Android device? My Download folder is next those one: Alarms, Android, data, DCIM, Documents, Movies, Music, Notifications, Pictures, ...

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

File manager showing my Download folder


Using this package path_provider I got those 3 paths:

/data/user/0/com.exemple.fonzo/cache
/data/user/0/com.exemple.fonzo/app_flutter
/storage/emulated/0

I cannot find or access those 3 folders from Solid-Explorer file manager on my un-rooted device GALAXY S8+ SM-G955F. Android 8.0. I just want to find the absolute path to a folder (like Download) that:

  • I can access with my Android file manager app.
  • I can write files in this folder from my flutter project.
like image 752
user2192870 Avatar asked Aug 09 '18 21:08

user2192870


3 Answers

I used this library to get public download directory in Android

ext_storage

import 'package:ext_storage/ext_storage.dart';

Future<String> _getPathToDownload() async {
  return ExtStorage.getExternalStoragePublicDirectory(
      ExtStorage.DIRECTORY_DOWNLOADS);
}

final String path = await _getPathToDownload();
print(path);

like image 164
Himanshu Verma Avatar answered Sep 18 '22 09:09

Himanshu Verma


I personaly use this method :

path_provider: 2.0.9
Future<String?> getDownloadPath() async {
    Directory? directory;
    try {
      if (Platform.isIOS) {
        directory = await getApplicationDocumentsDirectory();
      } else {
        directory = Directory('/storage/emulated/0/Download');
        // Put file in global download folder, if for an unknown reason it didn't exist, we fallback
        // ignore: avoid_slow_async_io
        if (!await directory.exists()) directory = await getExternalStorageDirectory();
      }
    } catch (err, stack) {
      print("Cannot get download folder path");
    }
    return directory?.path;
  }

Output :

  • On IOS it create a folder with the name of your app and put the file inside it. Users can easily find the folder, it's intuitive.
  • On Android, I test if the default download folder exist (it work most of the time), else I put inside the external storage directory (in this case, it's hard for the user to manually find the file...).
like image 23
Eng Avatar answered Sep 20 '22 09:09

Eng


You could use the downloads_path_provider package. You will have add <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> to your AndroidManifest.xml. Also if you plan to write into that folder and want your application to work for android version > 6 you must ask the user for writing permission. You could do that with https://pub.dev/packages/permission_handler. await PermissionHandler().requestPermissions([PermissionGroup.storage]);

like image 25
Kristiyan Gospodinov Avatar answered Sep 18 '22 09:09

Kristiyan Gospodinov