image_gallery_saver:基于 Flutter 的图片保存插件项目

可帮助 Flutter 开发者将图片、视频等文件保存到设备相册。支持网络和本地资源保存,提供质量设置、文件名自定义等功能,适配 iOS 和 Android 平台权限处理。【此简介由AI生成】

Branch1Tags0
FilesLast commitLast update
1 year ago
1 year ago
1 year ago
3 years ago
3 years ago
5 years ago
3 years ago
7 years ago
6 years ago
6 years ago
3 years ago
3 years ago
3 years ago
3 years ago

image_gallery_saver

构建状态
pub 包
许可证

我们使用 image_picker 插件从 Android 和 iOS 的图片库中选择图片,但它无法将图片保存到相册。本插件可提供此功能。

使用方法

要使用此插件,请在您的 pubspec.yaml 文件中添加 image_gallery_saver 作为依赖项。例如:

dependencies:
  image_gallery_saver: '^2.0.3'

iOS

您的项目需使用 Swift 创建。 将以下键值添加到 Info.plist 文件中,该文件位于 <项目根目录>/ios/Runner/Info.plist:

  • NSPhotoLibraryAddUsageDescription - 描述您的应用为何需要相册权限。在可视化编辑器中,此项称为「隐私 - 照片库添加用途说明」

Android

您需要申请存储权限以将图片保存至相册。可通过 flutter_permission_handler 处理存储权限。 在 Android 10 版本中,打开清单文件并在 application 标签内添加此行代码:

 <application android:requestLegacyExternalStorage="true" .....>

示例

从互联网保存图片,质量和名称可自定义

  _saveLocalImage() async {
    RenderRepaintBoundary boundary =
        _globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
    ui.Image image = await boundary.toImage();
    ByteData? byteData =
        await (image.toByteData(format: ui.ImageByteFormat.png));
    if (byteData != null) {
      final result =
          await ImageGallerySaver.saveImage(byteData.buffer.asUint8List());
      print(result);
    }
  }
  
  _saveNetworkImage() async {
    var response = await Dio().get(
        "https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=a62e824376d98d1069d40a31113eb807/838ba61ea8d3fd1fc9c7b6853a4e251f94ca5f46.jpg",
        options: Options(responseType: ResponseType.bytes));
    final result = await ImageGallerySaver.saveImage(
        Uint8List.fromList(response.data),
        quality: 60,
        name: "hello");
    print(result);
  }

从互联网保存文件(如:视频/GIF/其他)

  _saveNetworkGifFile() async {
    var appDocDir = await getTemporaryDirectory();
    String savePath = appDocDir.path + "/temp.gif";
    String fileUrl =
        "https://hyjdoc.oss-cn-beijing.aliyuncs.com/hyj-doc-flutter-demo-run.gif";
    await Dio().download(fileUrl, savePath);
    final result =
        await ImageGallerySaver.saveFile(savePath, isReturnPathOfIOS: true);
    print(result);
  }

  _saveNetworkVideoFile() async {
    var appDocDir = await getTemporaryDirectory();
    String savePath = appDocDir.path + "/temp.mp4";
    String fileUrl =
        "https://s3.cn-north-1.amazonaws.com.cn/mtab.kezaihui.com/video/ForBiggerBlazes.mp4";
    await Dio().download(fileUrl, savePath, onReceiveProgress: (count, total) {
      print((count / total * 100).toStringAsFixed(0) + "%");
    });
    final result = await ImageGallerySaver.saveFile(savePath);
    print(result);
  }

Introduction

可帮助 Flutter 开发者将图片、视频等文件保存到设备相册。支持网络和本地资源保存,提供质量设置、文件名自定义等功能,适配 iOS 和 Android 平台权限处理。【此简介由AI生成】

Customize your domain