简体   繁体   English

如何将 multi_image_picker2 `Asset ` 转换为 `File ` 对象?

[英]How to convert multi_image_picker2 `Asset ` to a `File ` object?

I am using package multi_image_picker2 to select multiple images.我正在使用包multi_image_picker2来选择多个图像。 Below is my code.下面是我的代码。

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;
    });
  }

I need to convert these to File objects, so I can upload them to Amazon S3.我需要将这些转换为File对象,以便我可以将它们上传到 Amazon S3。 But how can I convert this Asset object from multi_image_picker2 to a File object?但是如何将此Asset对象从multi_image_picker2转换为File对象?

You can simply use flutter_absolute_path:您可以简单地使用 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);
          }
        });
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM