android - 如何将 multi_image_picker2 `Asset` 转换为 `File` 对象?
问题描述
我正在使用包multi_image_picker2
来选择多个图像。下面是我的代码。
Future<void> loadAssets() async {
List<Asset> resultList = <Asset>[];
String error = 'No Error Detected';
try {
resultList = await MultiImagePicker.pickImages(
maxImages: 5,
enableCamera: true,
selectedAssets: images,
cupertinoOptions: CupertinoOptions(
takePhotoIcon: "chat",
doneButtonTitle: "Fatto",
),
materialOptions: MaterialOptions(
actionBarColor: "#abcdef",
actionBarTitle: "Pick Your Images",
allViewTitle: "All Photos",
useDetailsView: false,
//selectCircleStrokeColor: "#000000",
),
);
} on Exception catch (e) {
error = e.toString();
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
images = resultList;
//_error = error;
});
}
我需要将这些转换为File
对象,以便将它们上传到 Amazon S3。但是我怎样才能把这个Asset
对象转换multi_image_picker2
成一个File
对象呢?
解决方案
你可以简单地使用flutter_absolute_path:
List<File> allImages = [];
resultList.forEach((imageAsset) async {
await FlutterAbsolutePath.getAbsolutePath(imageAsset.identifier)
.then((filePath) {
File tempFile = File(filePath);
if (tempFile.existsSync()) {
allImages.add(tempFile);
}
});
});
推荐阅读
- kotlin - 在 Kotlin 中,制作字符串的最佳方法是什么?+ String?` 如果任何输入为 `null`,则返回 `null`?
- flutter - 在您的路径中找不到 git,互联网或外部命令无法识别
- javascript - 单击时复制到剪贴板
- javascript - Firefox 上的 JavaScript 错误:TypeError:无法重新定义不可配置的属性“userAgent”
- azure - OAUTH / Azure Functions:为不支持服务主体的端点验证 AAD 用户的方法
- java - Spring Boot Test:为每个单元测试注入不同的资源
- python - 无法为堆积条形图设置标签
- javascript - Chrome 中的菜单中断
- arrays - Powershell:如何将 -Split 单词数组通过管道传输到命令字符串中
- symfony - 在奏鸣曲编辑选项卡中编写基本 HTML