dart 如何在Flutter中获取下载路径?

neekobn8  于 2023-04-27  发布在  Flutter
关注(0)|答案(2)|浏览(233)

我使用下面的代码来获取下载文件夹的路径:

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

上面的代码生成了这个字符串:/storage/emulated/0/Downloads
但控制台说“没有这样的文件或目录”.我创建了这个路径字符串,因为堆栈溢出的一些答案提到,上面是下载文件夹的位置,但它不是.那么什么是Android上的下载文件夹的路径,以及如何访问它?

5uzkadbs

5uzkadbs1#

你可以复制粘贴下面运行完整的代码
下面的示例代码将文本文件写入Downloads文件夹
步骤1:您可以将WRITE_EXTERNAL_STORAGE添加到AndroidManifest.xml

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

步骤2:使用ext_storagehttps://pub.dev/packages/ext_storage包并使用https://pub.dev/packages/permission_handler 4.4.0请求权限

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

第四步:请求许可

void requestPermission() {  

//permission_handler 10.2.0
await Permission.manageExternalStorage.request();

//permission_handler 4.4.0
//PermissionHandler().requestPermissions([PermissionGroup.storage]);

  }

第5步:使用ExtStorage.getExternalStoragePublicDirectory获取Downloads文件夹

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

工作演示

全码

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),
      ),
    );
  }
}
9jyewag0

9jyewag02#

不需要任何软件包就可以得到下载路径。试试看:

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

相关问题