flutter复制assets工程文件到沙盒中
2021-07-14 本文已影响0人
smallLabel
// 拷贝demo到沙盒
Future<void> copyDemoToSandBox() async {
// 获取cache目录,
Directory cache = await getTemporaryDirectory();
final manifestContent = await rootBundle.loadString('AssetManifest.json');
final Map<String, dynamic> manifestMap = convert.json.decode(manifestContent);
// 这里自己过滤需要复制的文件夹
manifestMap.keys
.where((key) => key.contains('demo/') && !key.contains('.DS_'))
.forEach((element) async {
// 读取数据
ByteData data = await rootBundle.load(element);
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
String dataPath = path.join(cache.path, element);
File file = File(dataPath);
await file.create(recursive: true);
await File(dataPath).writeAsBytes(bytes);
});
}
复制前工程目录结构
复制后沙盒目录文件