FileSystemException: Cannot copy file (OS Error: Operation not permitted, errno = 1)

Issue

Since my migration to android 11, it is impossible for me to copy/move an image without using the "MANAGE_EXTERNAL_STORAGE" permission.

My code to store in tempDir:

void _takePicture() async {
    try {
      final p = await getTemporaryDirectory();

      print(p.path);
      final name = DateTime.now();
      print(name);
      final path = "${p.path}/$name.png";

      await controller.takePicture(path).then((value) {
        print('here');
        print(path);

        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => PreviewScreen(
                      imgPath: path,
                      fileName: "$name.png",
                      work: widget.work,
                      imgArr: widget.imgArr,
                    )));
      });
    } catch (e) {
      print(e);
    }
  }

My code when I try to copy the file from cache to Documents directory:

  upload(String imageFile, BuildContext ctx) async {
    try {
      // Temporary Image File
      File img = File(imageFile);
      bool exist = await img.exists();
      print(exist);
      // Output Directory
      Directory dir = await widget.work.getLocalPath();
      // Save the image in the allocated directory
      String path = dir.path + widget.fileName;
      File savedImage = await img.copy('$path');

      // Insert Into The List Of Current Recording Path
      widget.imgArr.add(savedImage.path);

      // Move to the wine view
      Navigator.pop(ctx);
      Navigator.pop(ctx, img);
    } catch (e) {
      print(e);
    }
  }

Solution

/storage/emulated/0/Documents/myapp/folder/folder/97897897/records/2022-03-30 08:26:14.098976.png

There is no reason why you would not be able to create a file in those subdirectories of public Documents folder. Be it that you create those subdirectories first. Just the usual WRITE permission needed. So no MANAGE_EXTERNAL_STORAGE needed.

But a filename like ....08:26:14.098976.png will not go as it contains forbidden characters :.

Replace them by someting more suitable.

Answered By – blackapps

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.