从flutter中的URL下载SVG图像

nr9pn0ug  于 2023-03-31  发布在  Flutter
关注(0)|答案(1)|浏览(169)

我试图从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';
    }
  }
}
xzlaal3s

xzlaal3s1#

使用Flutter 3.7.8软件包:davinci,flutter_svg,image_gallery_saver

import 'package:davinci/core/davinci_capture.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';

void main() {
  runApp(
    const MaterialApp(
      home: MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Future<void> _onClick(String url) async {
    final response = await Dio().get(url);

    final imageBytes = await DavinciCapture.offStage(
      SvgPicture.string(
        response.data,
        width: 200,
        height: 200,
        fit: BoxFit.contain,
      ),
      returnImageUint8List: true,
      openFilePreview: false,
      saveToDevice: false,
    );
    final result = await ImageGallerySaver.saveImage(
      imageBytes,
      name: "QRCode",
    );

    setState(() {
      isSuccess = result['isSuccess'];
    });
  }

  bool? isSuccess;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: isSuccess != null
            ? Text('isSuccess: $isSuccess')
            : ElevatedButton(
                onPressed: () => _onClick(
                    'https://qodevault-staging.s3.us-east-2.amazonaws.com/qrcodes/6136f35c3a5e9824329fdd8166d01715.svg'),
                child: const Text('Here'),
              ),
      ),
    );
  }
}

相关问题