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

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

自从我迁移到 android 11 后,我无法 copy/move 不使用“MANAGE_EXTERNAL_STORAGE”权限的图像。

我要存储在 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);
    }
  }

我尝试将文件从缓存复制到 Documents 目录时的代码:

  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);
    }
  }

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

没有理由不能在 public Documents 文件夹的子目录中创建文件。是您先创建这些子目录。只需要通常的 WRITE 权限。所以不需要 MANAGE_EXTERNAL_STORAGE。

但是像 ....08:26:14.098976.png 这样的文件名将不会被删除,因为它包含禁用字符 :

换成更合适的。