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?
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
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),
),
);
}
}
You can get download path without any package. Try that:
Directory dir = Directory('/storage/emulated/0/Download');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With