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