Flutter development practice - save pictures to album

Flutter development practice - save pictures to the album. Save the album using the image_gallery_saver plugin

1. Introduce the image_gallery_saver plugin

Introduce plugins in pubspec.yaml

  # 保存图片到相册
  image_gallery_saver: ^1.7.1

  # 权限
  permission_handler: ^10.0.0

2. Save the code to the photo album

Save pictures to gallery with image_gallery_saver

// 保存到相册的UTil
class SaveToAlbumUtil {
    
    
  static Future<dynamic> saveLocalImage(String imagePath) async {
    
    
    var image = await ImageUtil.loadImageByFile(imagePath);
    ByteData? byteData =
    await (image.toByteData(format: ui.ImageByteFormat.png));
    if (byteData != null) {
    
    
      final result =
      await ImageGallerySaver.saveImage(byteData.buffer.asUint8List());
      print("SaveToAlbumUtil result:${
      
      result}");
      return result;
    } else {
    
    
      throw StateError("saveLocalImage error imagePath:${
      
      imagePath}");
    }
  }

  static void saveNetworkImage(String imageUrl) async {
    
    
    var response = await Dio().get(
        imageUrl,
        options: Options(responseType: ResponseType.bytes));
    final result = await ImageGallerySaver.saveImage(
        Uint8List.fromList(response.data),
        quality: 60,
        name: "hello");
    print(result);
  }
}

3. Summary

Flutter development practice - save pictures to the album. Save the photo album using the image_gallery_saver plugin.

Learning records, keep improving every day.

Guess you like

Origin blog.csdn.net/gloryFlow/article/details/131948149