gpt4 book ai didi

flutter - 如何使用 Flutter 下载文件并将其存储在 Downloads 文件夹中

转载 作者:行者123 更新时间:2023-12-02 17:54:40 56 4
gpt4 key购买 nike

我正在使用 Flutter 1.10.3 构建应用程序,但将图像下载到设备的下载文件夹时遇到困难。

是否可以选择在不使用任何第三部分库的情况下执行此操作?

最佳答案

添加一些必需的依赖项,我有一个仅适用于 android 的测试演示。在 IOS 设备中实现时,请查看 file_utilspath_provider

  dio: ^3.0.0

path_provider: ^1.3.0

simple_permissions: ^0.1.9

file_utils: ^0.1.3

注意: simple_permissions 已断开连接,请使用此依赖项 permission_handler

您需要添加对 Android list 文件的权限。

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

示例代码:

  import 'dart:io';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:async';
import 'package:simple_permissions/simple_permissions.dart';
import 'package:file_utils/file_utils.dart';
import 'dart:math';

void main() => runApp(Downloader());

class Downloader extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
title: "File Downloader",
debugShowCheckedModeBanner: false,
home: FileDownloader(),
theme: ThemeData(primarySwatch: Colors.blue),
);
}

class FileDownloader extends StatefulWidget {
@override
_FileDownloaderState createState() => _FileDownloaderState();
}

class _FileDownloaderState extends State<FileDownloader> {

final imgUrl = "https://images6.alphacoders.com/683/thumb-1920-683023.jpg";
bool downloading = false;
var progress = "";
var path = "No Data";
var platformVersion = "Unknown";
Permission permission1 = Permission.WriteExternalStorage;
var _onPressed;
static final Random random = Random();
Directory externalDir;

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


Future<void> downloadFile() async {
Dio dio = Dio();
bool checkPermission1 =
await SimplePermissions.checkPermission(permission1);
// print(checkPermission1);
if (checkPermission1 == false) {
await SimplePermissions.requestPermission(permission1);
checkPermission1 = await SimplePermissions.checkPermission(permission1);
}
if (checkPermission1 == true) {
String dirloc = "";
if (Platform.isAndroid) {
dirloc = "/sdcard/download/";
} else {
dirloc = (await getApplicationDocumentsDirectory()).path;
}

var randid = random.nextInt(10000);

try {
FileUtils.mkdir([dirloc]);
await dio.download(imgUrl, dirloc + randid.toString() + ".jpg",
onReceiveProgress: (receivedBytes, totalBytes) {
setState(() {
downloading = true;
progress =
((receivedBytes / totalBytes) * 100).toStringAsFixed(0) + "%";
});
});
} catch (e) {
print(e);
}

setState(() {
downloading = false;
progress = "Download Completed.";
path = dirloc + randid.toString() + ".jpg";
});
} else {
setState(() {
progress = "Permission Denied!";
_onPressed = () {
downloadFile();
};
});
}
}

@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text('File Downloader'),
),
body: Center(
child: downloading
? Container(
height: 120.0,
width: 200.0,
child: Card(
color: Colors.black,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
SizedBox(
height: 10.0,
),
Text(
'Downloading File: $progress',
style: TextStyle(color: Colors.white),
),
],
),
),
)
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(path),
MaterialButton(
child: Text('Request Permission Again.'),
onPressed: _onPressed,
disabledColor: Colors.blueGrey,
color: Colors.pink,
textColor: Colors.white,
height: 40.0,
minWidth: 100.0,
),
],
)));
}

enter image description here

关于flutter - 如何使用 Flutter 下载文件并将其存储在 Downloads 文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57995621/

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