如何在flutter中将图像下载到图库

问题描述 投票:0回答:2

我正在使用学校管理应用程序,在这里我需要将图像和视频发送到应用程序,并允许用户将图像和视频下载到他们的手机图库。我被这个困住了! 请帮助我..谢谢..

image flutter dart file-upload flutter-layout
2个回答
5
投票

这是可能有帮助的功能

saveImage(String url) async {
    
    var response = await Dio()
        .get(url, options: Options(responseType: ResponseType.bytes));
    final result = await ImageGallerySaver.saveImage(
        Uint8List.fromList(response.data),
        quality: 100,
        name: "${widget.id}");
  }

为了使用此功能,您必须安装两个软件包(并将它们放在您的 pubspec.ymal 文件中):

dio: ^3.0.10
image_gallery_saver: '^1.5.0'

0
投票
 ElevatedButton(
              onPressed:()async{
                try{
                  // Get the ByteData for the asset image
                  ByteData data = await rootBundle.load('backgrounds/$nameOfImage.jpg');
                  List<int> bytes = data.buffer.asUint8List();

                  // Get the external storage directory
                  Directory? externalStorageDirectory = await getExternalStorageDirectory();

                  // Create a File object with the appropriate path
                  File file = File(path.join(externalStorageDirectory!.path, '$nameOfImage.jpg'));

                  // Write the image bytes to the file
                  await file.writeAsBytes(bytes);

                  // Save the image to the gallery
                  await ImageGallerySaver.saveFile(file.path, isReturnPathOfIOS: true);

                  // Show a dialog indicating success
                  showDialog(
                    context: context,
                    builder: (BuildContext context) => AlertDialog(
                      title: const Text('Image saved to gallery successfully'),
                    ),
                  );
                }catch (e) {
                      print('Error: $e');
                      // Handle the error (show a snackbar, display an error message, etc.)
                            }
                          },
              child: Text('Download')),
© www.soinside.com 2019 - 2024. All rights reserved.