【发布时间】:2021-12-11 09:08:07
【问题描述】:
我希望我的应用在安装时首先在内部存储中创建特定目录,然后单击按钮将特定小部件(屏幕)转换为 image.png,然后将其保存在创建的目录中。
我需要完整的代码。我一直在寻找,但没有找到有效的方法。
【问题讨论】:
标签: image flutter directory save internal-storage
我希望我的应用在安装时首先在内部存储中创建特定目录,然后单击按钮将特定小部件(屏幕)转换为 image.png,然后将其保存在创建的目录中。
我需要完整的代码。我一直在寻找,但没有找到有效的方法。
【问题讨论】:
标签: image flutter directory save internal-storage
这个功能是您截取小部件并保存在本地所需的全部:
static GlobalKey _repaintKey = GlobalKey();
Widget _yourWidget(){
return Stack(
key: _repaintKey,
children: [
],
);
}
Future<void> _takeScreenShot(context) async {
RenderRepaintBoundary boundary = _repaintKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
final path = join(
(await getTemporaryDirectory()).path, "screenshot${DateTime.now().toIso8601String()}.png");
File imgFile = File(path);
imgFile.writeAsBytes(pngBytes).then((value) {
Navigator.of(context).pushNamed(Routes.uploadImage, arguments: value.uri.path);
}).catchError((onError) {
print(onError);
});
}
【讨论】: