Android 如何确保资源被释放(或者:flatter中的缓存如何工作)
给定段的范围是允许用户在拍摄照片后手动绘制的摄影机。我用它来获取存储路径,我用它来拍摄和存储照片。之后,我使用旋转图片(如本答案所示:) 现在,当我拍摄照片时,一切正常,但离开后,重新进入cameraMode并拍摄新照片,旧照片会显示出来。系统报告Android 如何确保资源被释放(或者:flatter中的缓存如何工作),android,flutter,dart,Android,Flutter,Dart,给定段的范围是允许用户在拍摄照片后手动绘制的摄影机。我用它来获取存储路径,我用它来拍摄和存储照片。之后,我使用旋转图片(如本答案所示:) 现在,当我拍摄照片时,一切正常,但离开后,重新进入cameraMode并拍摄新照片,旧照片会显示出来。系统报告资源未能调用release,我认为这就是问题所在。是否有办法确保在保存我的旋转图片时,删除/释放旧图片?作为对这种情况的修复,我可能会将第一个图像保存在一个临时位置,然后只保存“固定”图像,但我还对底层引擎如何处理这类事情感兴趣 颤振医生-v (是的,
资源未能调用release
,我认为这就是问题所在。是否有办法确保在保存我的旋转图片时,删除/释放旧图片?作为对这种情况的修复,我可能会将第一个图像保存在一个临时位置,然后只保存“固定”图像,但我还对底层引擎如何处理这类事情感兴趣
颤振医生-v
(是的,我知道android studio显然没有dart和Flatter插件,但代码是经过编译的,我真的不知道为什么Flatter没有意识到这一点)
编辑:忘了提到有类似的帖子
将错误声明为Android系统消息(这很有意义),但问题(和答案)集中在如何检查此错误消息上,虽然我很清楚问题在哪里,但不确定如何修复它,也不确定代码产生此错误的原因。同时,我想知道在iOS上会发生什么,因为从技术上讲,flatter代码也应该在iOS上运行(尽管不确定,因为我只为android开发)
还提到了如何显示stacktrace(这与SO帖子中的一个答案相同)
[✓] Flutter (Channel stable, 1.22.2, on Linux, locale en_US.UTF-8)
• Flutter version 1.22.2 at /opt/flutter
• Framework revision 84f3d28555 (2 weeks ago), 2020-10-15 16:26:19 -0700
• Engine revision b8752bbfff
• Dart version 2.10.2
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at /home/brahms/Android/Sdk
• Platform android-30, build-tools 30.0.2
• Java binary at: /opt/android-studio/jre/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[!] Android Studio
• Android Studio at /opt/android-studio
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
[✓] Connected device (1 available)
• <redacted>
• android-arm64
• Android 9 (API 28)
await _initializeControllerFuture;
path = join(externalStoragePath, 'photo.png');
// if the file exists we need to delete it, because the camera cant overwrite
if (await File(path).exists()) File(path).delete();
await _controller.takePicture(path);
// originally the photo is stored with 90° rotated. So we fix this manually
img.Image capturedImage = img.decodeImage(await File(path).readAsBytes());
img.Image orientedImage = img.bakeOrientation(capturedImage);
File(path).writeAsBytes(img.encodePng(orientedImage));
// If the picture was taken, display it on screen.
setState(() {
cameraBody = cameraImage(path);
});
[..]
// used to display the picture over the whole parentWidget body
Widget cameraImage(String imagePath) {
return Container(
width: double.infinity,
child: Image.file(File(imagePath), fit: BoxFit.fill),
);
}