我试图从URL下载SVG图像。我试图将图像转换为Uint8List
,然后使用ImageGallerySaver下载图像。
但我收到一个错误
PlatformException(PlatformException(error,decodeByteArray(image,0,image.size)不能为null,null,java.lang.NullPointerException:decodeByteArray(图像,0,图像大小)不能为空
同样的代码工程,如果我尝试下载PNG格式的图像。
以下是我使用的代码。
String url = 'https://qodevault-staging.s3.us-east-2.amazonaws.com/qrcodes/6136f35c3a5e9824329fdd8166d01715.svg'
final bytes = await kNetworkImageToBytes(url);
kSaveImageFromBytes(imgData: bytes, name: 'qrcode');
函数将网络图像解析为字节
///converts network image (Except for svg) to [Uint8List]
Future<Uint8List> kNetworkImageToBytes(String imageUrl) async {
try {
var response = await Dio().get(
imageUrl,
options: Options(responseType: ResponseType.bytes),
);
return Uint8List.fromList(response.data);
} catch (e) {
rethrow;
}
}
从字节下载图像的函数
Future<void> kSaveImageFromBytes({
required Uint8List imgData,
required String name,
bool appendRandomString = true,
}) async {
final random = generateRandomString(10);
final filename = appendRandomString ? '${name}_$random' : name;
final bool hasPermission =
await PermissionHandler.requestPermission(Permission.storage);
if (!hasPermission) throw 'Permission not granted';
final result = await ImageGallerySaver.saveImage(
imgData,
quality: 100,
name: 'QV_$filename',
);
print(result);
if (result is Map) {
if (result['isSuccess'].toString() == 'false') {
throw 'Error saving image';
}
}
}
1条答案
按热度按时间xzlaal3s1#
使用Flutter 3.7.8软件包:davinci,flutter_svg,image_gallery_saver