【问题标题】:Flutter imagePicker to Uint8listFlutter imagePicker 到 Uint8list
【发布时间】:2021-10-13 15:23:21
【问题描述】:

所以我想将我的图像存储到列表中,但我无法将其转换为 Uint8list 有没有办法在颤振中使用其他选项,例如 RootBundle ???

为什么不使用 RootBundler ?? 问题是 RootBundle 需要在 Asset 部分下的 Pubspec.yaml 上声明 但是我要转换的图像位于 App 临时目录中,我无法更改 所以 RootBundle 无法访问并且总是抛出 Asset is null (不是),因为它没有在 pubspec.yaml 中声明

static Future pickImage(PickType type) async {
    try {
      var photo = await ImagePicker().pickImage(
        source:
            type == PickType.Camera ? ImageSource.camera : ImageSource.gallery,
        imageQuality: 80,
      );
      var photosPicked;
      print(photo!.name);
      Directory tempDir = await getTemporaryDirectory();
      print(tempDir.path);
      rootBundle
          .load('${tempDir.path}/${photo.name}')
          .then((value) => photosPicked = value);
      return photosPicked;
    } catch (e) {
      print(e);
    }
  }

【问题讨论】:

标签: list flutter


【解决方案1】:

我通过使用 ReadAsByte 函数弄明白了

static Future pickImage(PickType type) async {
    try {
      var photo = await ImagePicker().pickImage(
        source:
            type == PickType.Camera ? ImageSource.camera : ImageSource.gallery,
        imageQuality: 80,
      );

      File imageFile = File(photo!.path);
      Uint8List imageRaw = await imageFile.readAsBytes();
      return imageRaw;
    } catch (e) {
      print(e);
    }
  }

【讨论】:

    猜你喜欢
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2020-12-21
    • 2022-01-11
    • 2020-10-26
    • 2021-02-07
    • 2020-03-10
    相关资源
    最近更新 更多