如何在Dart中将'ByteData'实例写入文件?
我使用flatter将一个“资产”加载到如何在Dart中将'ByteData'实例写入文件?,dart,Dart,我使用flatter将一个“资产”加载到文件中,以便本机应用程序可以访问它 这是我加载资产的方式: final dbBytes = await rootBundle.load('assets/file'); 这将返回字节数据的实例 如何将其写入dart.io.File实例?是对以下内容的抽象: 一种固定长度的随机存取字节序列,它还提供 对固定宽度整数和浮点数的随机和未对齐访问 由这些字节表示的点号 正如Gunter在评论中提到的,您可以使用。但是,从字节数据到列表确实需要一些API工作 imp
文件中
,以便本机应用程序可以访问它
这是我加载资产的方式:
final dbBytes = await rootBundle.load('assets/file');
这将返回字节数据的实例
如何将其写入dart.io.File
实例?是对以下内容的抽象:
一种固定长度的随机存取字节序列,它还提供
对固定宽度整数和浮点数的随机和未对齐访问
由这些字节表示的点号
正如Gunter在评论中提到的,您可以使用。但是,从字节数据
到列表
确实需要一些API工作
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
Future<void> writeToFile(ByteData data, String path) {
final buffer = data.buffer;
return new File(path).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
导入'dart:async';
导入“dart:io”;
导入“dart:键入的_数据”;
未来写入文件(字节数据、字符串路径){
最终缓冲区=data.buffer;
返回新文件(路径)。writeAsBytes(
buffer.asUint8List(data.offsetingbytes,data.lengthInBytes));
}
我还需要让关于颤振的文档对于这个用例更加清晰。要搜索flatter ByteData来列出
,然后在这里找到,但没有完全回答我的问题:
如何将字节数据
转换为列表
自我调查后,解决方案为:
使用.cast()
然后,您需要安装软件包
这应该起作用:
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';
final dbBytes = await rootBundle.load('assets/file'); // <= your ByteData
//=======================
Future<File> writeToFile(ByteData data) async {
final buffer = data.buffer;
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
var filePath = tempPath + '/file_01.tmp'; // file_01.tmp is dump file, can be anything
return new File(filePath).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
//======================
导入'dart:async';
导入“dart:io”;
导入“dart:键入的_数据”;
导入“package:path_provider/path_provider.dart”;
final dbBytes=wait rootBundle.load('assets/file');// 对于那些希望写入字节(也称为Uint8List)而不是ByteData的用户,请注意
从/runtime/lib/typed_data.patch:
@patch
class ByteData implements TypedData {
@patch
@pragma("vm:entry-point")
factory ByteData(int length) {
final list = new Uint8List(length) as _TypedList;
_rangeCheck(list.lengthInBytes, 0, length);
return new _ByteDataView(list, 0, length);
}
@patch
class Uint8List {
@patch
@pragma("vm:exact-result-type", _Uint8List)
factory Uint8List(int length) native "TypedData_Uint8Array_new";
}
如果您使用的是后一种类型,您可以使用Rami提供的答案,并按如下方式修改报税表:
导入'dart:async';
导入“dart:io”;
导入“dart:键入的_数据”;
导入“package:path_provider/path_provider.dart”;
未来写入文件(Uint8List数据)异步{
(...)
返回新文件(文件路径).writeAsBytes(数据);
}
啊,太酷了。最后我做了destinationFile.writeAsBytes(dbBytes.buffer.asUint8List())
(在调试器中,我看到dbBytes
包含Uint8List
)。我想这和你建议的一样,或者一个比另一个更有效?记住,ByteData
可能是缓冲区的一部分,所以你可能不想写整个缓冲区。我已经更新了这个示例来说明这一点。由于某种原因,您的writeToFile
无法处理超过665MB的数据。你知道我是否需要设置gradle.properties或类似的属性吗?或者您对如何编写大型文件还有其他建议吗?@matanlurey您给出了什么路径?Uint8List是一个列表:抽象类Uint8List实现了列表感谢您精心设计的解决方案,但如何在flift中加密/解密bytedata
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';
final dbBytes = await rootBundle.load('assets/file'); // <= your ByteData
//=======================
Future<File> writeToFile(ByteData data) async {
final buffer = data.buffer;
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
var filePath = tempPath + '/file_01.tmp'; // file_01.tmp is dump file, can be anything
return new File(filePath).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
//======================
var file;
try {
file = await writeToFile(dbBytes); // <= returns File
} catch(e) {
// catch errors here
}
@patch
class ByteData implements TypedData {
@patch
@pragma("vm:entry-point")
factory ByteData(int length) {
final list = new Uint8List(length) as _TypedList;
_rangeCheck(list.lengthInBytes, 0, length);
return new _ByteDataView(list, 0, length);
}
@patch
class Uint8List {
@patch
@pragma("vm:exact-result-type", _Uint8List)
factory Uint8List(int length) native "TypedData_Uint8Array_new";
}