flutter 如何将assetImage转换为Uint 8List/文件

qfe3c7zg  于 2023-01-02  发布在  Flutter
关注(0)|答案(1)|浏览(322)

我正在尝试将带有链接的assetImage(“assets/image.png”)转换为文件/uint 8List/

final ByteData byteData = await rootBundle.load(logoUrl);
final File assetLogoFile =
File("${(await getTemporaryDirectory()).path}/$logoUrl");

File markerLogoFile = await assetLogoFile.writeAsBytes(byteData.buffer
        .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

final Uint8List logoUint8List = await markerLogoFile.readAsBytes();

我收到以下错误;

E/flutter ( 5136): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.example.project/cache/assets/images/flare.png' (OS Error: No such file or directory, errno = 2)
E/flutter ( 5136): #0      _File.open.<anonymous closure> (dart:io/file_impl.dart:356:9)
E/flutter ( 5136): #1      _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter ( 5136): #2      _CustomZone.runUnary (dart:async/zone.dart:1265:19)
carvr3hs

carvr3hs1#

您可以使用以下代码将资产文件转换为Uint 8List;

final ByteData bytes = await rootBundle.load('assets/image.jpg');
 final Uint8List list = bytes.buffer.asUint8List();

相关问题