Flutter : save image in gallery

688 Views Asked by At

I wish to store images in the gallery. However, this functionality only seems to work during debugging when I build the APK and test it on my phone. Once I install the APK without debugging, the image-saving feature no longer functions. It's important to note that I already have the necessary permissions to access local storage.The permissions

I tested it on two versions of Android 12 and 13

var image = await boundary.toImage();
    var byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    var ref = FFAppState().FrameRef;
    final name = 'cl1box1_' + ref;
    await ImageGallerySaver.saveImage(byteData!.buffer.asUint8List(),
        name: name);
1

There are 1 best solutions below

0
AudioBubble On

Try this

use this below plugin

  1. device_info_plus: ^9.0.2

  2. permission_handler: ^10.4.3

  3. image_gallery_saver: ^2.0.3

    saveImage() async {
    PermissionStatus result;
    if (Platform.isAndroid) {
      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      if (androidInfo.version.sdkInt >= 33) {
        result = await Permission.photos.request();
      } else {
        result = await Permission.storage.request();
      }
    if (result.isGranted) {
    
    var image = await boundary.toImage();
    var byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    var ref = FFAppState().FrameRef;
    final name = 'cl1box1_' + ref;
    await ImageGallerySaver.saveImage(byteData!.buffer.asUint8List(),
        name: name);
      }
    
    } else {
      await openAppSettings();
    }
     }
    

add this code on AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>