我需要在捕获图像后立即向图像添加文本。为此,我需要访问图像(相机默认保存它们的目录)。该应用程序请求许可罚款。
Future<void> _initializePhotoWatcher() async {
if (await Permission.storage.isGranted) {
// This gets the primary external storage directory, which may not directly be accessible in Android 10+
Directory? externalDir = await getExternalStorageDirectory();
String cameraPath =
'${externalDir?.path}/DCIM/Camera'; // This is a common path, but might vary
final dir = Directory(cameraPath);
if (await dir.exists()) {
final watcher = DirectoryWatcher(cameraPath);
watcher.events.listen((event) async {
if (event.type == ChangeType.ADD) {
final file = File(event.path);
if (await file.exists()) {
_addStampToPhoto(file);
}
}
});
} else {
print("Camera directory does not exist or is not accessible");
}
} else {
print("Need access to storage.");
}
}
此代码始终返回“相机目录不存在或不可访问”行,这意味着无法访问相机位置(图像)。如何访问图像并向其添加文本?这需要本机代码吗?
所以我在您的代码中看到的问题是 Path_Providers getExternalStorageDirectory 返回应用程序的目录。例如:'/storage/emulated/0/Android/data/com.foo.foo/files'。因此,通过将 /DCIM/Camera 添加到该路径,您将尝试访问(可能)不存在的字典。对于 Android,您只需使用:
String cameraPath = "/storage/emulated/0/DCIM/Camera/";
final dir = Directory(cameraPath);
这段代码应该按照 Android 的预期工作,尽管我对 iOS 真的一无所知,所以我无法帮助你,抱歉......