I'm newbie in "flutter" and I have question. It's possible to make copy of image and save it in device ?

  File image;

  picker() async {
    print('Picker is called');
    File img = await p.ImagePicker.pickImage(source: p.ImageSource.gallery);
    if (img != null) {
      image = img;
      setState(() {});
    }
  }

  save() async {
    print('Save is called');
    final Directory path = await image.parent;

    setState(() {});
  }

I have my image and I can get path of directory. But how to save this image in this directory but with another name ? Let's say my picture is called x.jpg and I want to have the same picture saved as y.jpg without delete of x.jpg


Solution 1: Shyju M

Use simple_permissions plugin to get the permission to write to the external storage.

Use path_provider plugin to get the path to the external storage folder

Finally use the image plugin to write the image

sample code

Future<Io.File> saveImage(Io.File file) async {
  try {
    var dir = await getExternalStorageDirectory();
    var testdir =
        await new Io.Directory('${dir.path}/testfolder').create(recursive: true);
    IM.Image image = IM.decodeImage(file.readAsBytesSync());
    return new Io.File(
        '${testdir.path}/${DateTime.now().toUtc().toIso8601String()}.png')
      ..writeAsBytesSync(IM.encodePng(image));
  } catch (e) {
    print(e);
    return null;
  }
}

also check the permission

void checkPer() async {
    bool checkResult = await SimplePermissions.checkPermission(
        Permission.WriteExternalStorage);
    if (!checkResult) {
      var status = await SimplePermissions.requestPermission(
          Permission.WriteExternalStorage);
      //print("permission request result is " + resReq.toString());
      if (status == PermissionStatus.authorized) {
        //saveimage
      }
    } else {
      //saveimage
    }
  }

namespaces

import 'dart:io' as Io;
import 'package:image/image.dart' as IM;
import 'package:path_provider/path_provider.dart';
import 'package:simple_permissions/simple_permissions.dart';