上一篇讲解了截图和保存图片的方法,这篇博客将对图片转base64和上传sentry,以及动态获取本地app版本号的方法做讲解。
首先来看下代码部分:
对图片做base64处理:
Future<String> base64Img() async {
try {
RenderRepaintBoundary boundary =
imgKey.currentContext.findRenderObject();
var image = await boundary.toImage(pixelRatio: 0.2);
ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
String imgStr = base64Encode(pngBytes);
return 'data:image/png;base64,' + imgStr;
} catch (e) {
print(e);
}
return 'Convert error!';
}
上传base64字符串到sentry:
Future<Null> _reportError(dynamic error, dynamic stackTrace) async {
if (isInDebugMode) {
print(stackTrace);
return;
}
base64Img().then((value) async {
print(value);
final SentryResponse response = await _sentry.capture(
event: Event(
exception: error,
stackTrace: stackTrace,
///获取本地版本号
release: parseVersionToName(Global.appVersion),
extra: {
'img' : value,
'uid' : userId ?? '0',
'uuid' : deviceId ?? '0000-0000-0000-0000'
}
),
);
// 上报结果处理
if (response.isSuccessful) {
print('Success! Event ID: ${response.eventId}');
} else {
print('Failed to report to Sentry.io: ${response.error}');
}
});
}
获取版本号:
一开始的本地版本号是写在sentry初始化的时候:
final SentryClient _sentry = new SentryClient(
dsn: dsn,
environmentAttributes: Event(
release: 'xxxx'
),
);
但是由于初始化的时候还没有进runApp,flutter还没来得及和app本地进行交互,根本拿不到版本号,所以这边对sentry进行了解后,发现可以在上传错误的时候再填写版本号,这时候交互都已经完成,直接通过MethodChannel去拿本地的版本号就可以,建议提前获取后存在flutter,方便使用。
另外,extra中还可以扩展其他的字段,把有需要额信息都可以放进去,方便开发者快速定位问题,提高代码质量。