Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Downloads path in Flutter?

Tags:

flutter

dart

I am using the below code to get the path to Downloads folder:

final output = await getExternalStorageDirectory();
RegExp pathToDownloads = new RegExp(r'.+0\/');
final outputPath = '${pathToDownloads.stringMatch(output.path).toString()}Downloads';

The above code generates this string: /storage/emulated/0/Downloads

But console says "No such file or directory". I created this path string because some answers on stack overflow mention that the above is the location of the downloads folder but it isn't. So what is the path to downloads folder on android and how to get access to it?

like image 214
gegobyte Avatar asked Mar 03 '23 11:03

gegobyte


2 Answers

You can copy paste run full code below
The following example code write a text file to Downloads folder

Step 1: You can add WRITE_EXTERNAL_STORAGE to AndroidManifest.xml

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

Step 2: Use package ext_storage https://pub.dev/packages/ext_storage and Request permission with https://pub.dev/packages/permission_handler 4.4.0

dependencies:
  flutter:
    sdk: flutter
  ext_storage: any
  permission_handler: 4.4.0 

Step 4: Request permission

 void requestPermission() {
    PermissionHandler().requestPermissions([PermissionGroup.storage]);
  }

Step 5: get Downloads folder with ExtStorage.getExternalStoragePublicDirectory

String path = await ExtStorage.getExternalStoragePublicDirectory(
        ExtStorage.DIRECTORY_DOWNLOADS);

working demo

enter image description here

full code

import 'package:flutter/material.dart';
import 'package:ext_storage/ext_storage.dart';
import 'dart:io';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() async {
    String path = await ExtStorage.getExternalStoragePublicDirectory(
        ExtStorage.DIRECTORY_DOWNLOADS);
    print(path);
    File file = await File('$path/counter.txt');
    file.writeAsString('this is test');

    setState(() {
      _counter++;
    });
  }

  void requestPermission() {
    PermissionHandler().requestPermissions([PermissionGroup.storage]);
  }

  @override
  void initState() {
    requestPermission();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
like image 51
chunhunghan Avatar answered Mar 08 '23 09:03

chunhunghan


You can get download path without any package. Try that:

Directory dir = Directory('/storage/emulated/0/Download');
like image 45
Aaydin Avatar answered Mar 08 '23 11:03

Aaydin