user2192870
user2192870

Reputation: 569

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

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:

Upvotes: 31

Views: 72463

Answers (8)

Himanshu Verma
Himanshu Verma

Reputation: 71

Note: This package is deprecated as of Feb 2022. (See Github repo readme)


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);

Upvotes: 6

Priyanshu Jindal
Priyanshu Jindal

Reputation: 871

Using path provider package, you should be able to do this:

import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;

String? downloadDirectory;
if (Platform.isAndroid) {
  final externalStorageFolder = await getExternalStorageDirectory();
  if (externalStorageFolder != null) {
    downloadDirectory = p.join(externalStorageFolder.path, "Downloads");
  }
} else {
  final downloadFolder = await getDownloadsDirectory();
  if (downloadFolder != null) {
    downloadDirectory = downloadFolder.path;
  }
}

Upvotes: 2

Eng
Eng

Reputation: 1724

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...).

For IOS : (Mentioned by @Wai Yan) Maybe add the follwing key in your plist.info to better see your app in document folder (https://stackoverflow.com/a/74457977/10088439).

UISupportsDocumentBrowser

Upvotes: 34

Mo Ali
Mo Ali

Reputation: 685

I simplified it without plugins I used this code and it's working like a charm

Directory dir = Directory('/storage/emulated/0/Download');

Upvotes: 4

Jairo Rodriguez
Jairo Rodriguez

Reputation: 396

In Flutter

https://pub.dev/packages/ext_storage

Installation Add ext_storage as a dipendency in your project pubspeck.yaml.

dependencies:
  ext_storage:

Usage First, you write import ext_storage package.

import 'package:ext_storage/ext_storage.dart';
    void dirpath() async {
      var path = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);
    }

Upvotes: 2

Kristiyan Gospodinov
Kristiyan Gospodinov

Reputation: 586

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]);

Upvotes: 5

Alex Ortiz
Alex Ortiz

Reputation: 49

Change the function getApplicationDocumentsDirectory() to getExternalStorageDirectory() it should show the external directory for the app.

Upvotes: 4

Rubens Melo
Rubens Melo

Reputation: 3315

You should use native feature.

At time, to access phone directory is provided by path_provider package .

With it, you can acceess: temporary directory, app directory, external storage.

Doc: https://pub.dartlang.org/packages/path_provider

Upvotes: 2

Related Questions