dart 如何在Flutter中下载图片到画廊

bxgwgixi  于 2024-01-03  发布在  Flutter
关注(0)|答案(2)|浏览(199)

我正在与学校管理应用程序,在这里我需要发送图像和视频到应用程序,并允许用户下载图像和视频到他们的手机画廊.我坚持这一点!请帮助我。.谢谢。.

gj3fmq9x

gj3fmq9x1#

下面是可能有所帮助的函数

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

字符串
为了使用这个功能,你必须安装两个软件包(在你的pubspec.ymal文件中):

  1. dio: ^3.0.10
  2. image_gallery_saver: '^1.5.0'

展开查看全部
tjjdgumg

tjjdgumg2#

  1. ElevatedButton(
  2. onPressed:()async{
  3. try{
  4. // Get the ByteData for the asset image
  5. ByteData data = await rootBundle.load('backgrounds/$nameOfImage.jpg');
  6. List<int> bytes = data.buffer.asUint8List();
  7. // Get the external storage directory
  8. Directory? externalStorageDirectory = await getExternalStorageDirectory();
  9. // Create a File object with the appropriate path
  10. File file = File(path.join(externalStorageDirectory!.path, '$nameOfImage.jpg'));
  11. // Write the image bytes to the file
  12. await file.writeAsBytes(bytes);
  13. // Save the image to the gallery
  14. await ImageGallerySaver.saveFile(file.path, isReturnPathOfIOS: true);
  15. // Show a dialog indicating success
  16. showDialog(
  17. context: context,
  18. builder: (BuildContext context) => AlertDialog(
  19. title: const Text('Image saved to gallery successfully'),
  20. ),
  21. );
  22. }catch (e) {
  23. print('Error: $e');
  24. // Handle the error (show a snackbar, display an error message, etc.)
  25. }
  26. },
  27. child: Text('Download')),

字符串

展开查看全部

相关问题