首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flutter imagePicker to Uint8list

Flutter imagePicker to Uint8list
EN

Stack Overflow用户
提问于 2021-08-10 02:30:08
回答 1查看 249关注 0票数 0

所以我想存储我的图像到列表,但我不能将它转换为Uint8list,在flutter中有一种方法可以使用另一个选项,如RootBundle?

为什么不使用RootBundler ??问题是RootBundle需要在资产部分下的Pubspec.yaml上声明,但我要转换的图像在应用程序临时目录中,这是我无法更改的,因此RootBundle无法访问并始终抛出资产为空(这不是),因为它没有在pubspec.yaml中声明

代码语言:javascript
复制
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);
    }
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-22 21:51:50

我是用ReadAsByte函数算出来的

代码语言:javascript
复制
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);
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68720430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档