【问题标题】:Get selected image path through Asset in flutter?在颤动中通过资产获取选定的图像路径?
【发布时间】:2021-08-07 14:16:41
【问题描述】:
我使用multi_image_picker: 从内部存储中选择图像。这个库返回List of Asset,但是我想作为图片路径,我用FlutterAbsolutePath
var path = await FlutterAbsolutePath.getAbsolutePath(resultList[i].identifier);
但这不支持 null 安全性,如何使用类似库获取图像路径?
【问题讨论】:
标签:
flutter
dart
imagepicker
【解决方案1】:
FlutterAbsolutePath 不支持 null 安全
我使用按钮点击事件从资源中获取图片路径
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () async {
final key = DateTime.now().toString();
File file = await getImageFileFromAssets(images[1]);
for(int i=0;i<images.length;i++)
{
print('# file path is ${file.path} #');
}
},
),
将Asset转换为图片路径的方法
Future<File> getImageFileFromAssets(Asset asset) async {
final byteData = await asset.getByteData();
final tempFile =
File("${(await getTemporaryDirectory()).path}/${asset.name}");
final file = await tempFile.writeAsBytes(
byteData.buffer
.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes),);
return file;
}