gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:49:39 24 4
gpt4 key购买 nike

我正在使用以下代码获取下载文件夹的路径:

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

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

最佳答案

您可以在下面复制粘贴运行完整代码
以下示例代码将文本文件写入 Downloads 文件夹

第 1 步:您可以将 WRITE_EXTERNAL_STORAGE 添加到 AndroidManifest.xml

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

第 2 步:使用包 ext_storage https://pub.dev/packages/ext_storagehttps://pub.dev/packages/permission_handler 4.4.0 请求权限
dependencies:
flutter:
sdk: flutter
ext_storage: any
permission_handler: 4.4.0

第 4 步:请求许可
 void requestPermission() {
PermissionHandler().requestPermissions([PermissionGroup.storage]);
}

第 5 步:使用 Downloads 获取 ExtStorage.getExternalStoragePublicDirectory 文件夹
String path = await ExtStorage.getExternalStoragePublicDirectory(
ExtStorage.DIRECTORY_DOWNLOADS);

工作演示

enter image description here

完整代码
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),
),
);
}
}

关于flutter - 如何在 Flutter 中获取下载路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61580392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com