dart 最大大小为5 MB的图像拾取器

f0ofjuux  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(156)

我在flutter中使用图像选择器包,因此我想提到所选图像的最大大小应为5 MB,在该包中有一个名为imagequity的参数,取值为0-100,但不等于5 MB

getImageFromGallery() async {
    try {
      var pickedfiles = await imagePicker.pickMultiImage(imageQuality: 5);

      if (pickedfiles.isNotEmpty) {

        


        for (int i = 0; i < pickedfiles.length; i++) {
          previewList.add(pickedfiles[i].path);
        }
        setState(() {});
      }
    } catch (e) {
      log(e.toString());
    }
  }
km0tfn4u

km0tfn4u1#

试试这个,我希望这能帮上忙

selectImagesFromImagePicker() async{
  // e.g 
  final knowSize = await defindSize(fileSelected, 2);
            if (double.parse(knowSize.toString()) > 5.0) {
              SomeSnackBar("File too large", "Limit is 5 mb below");
            } else {
              log("$knowSize mb", name: "FILE SIZE");
              filesChosen.add(File(x!));
            }
}  
  // Function to know the size of a certain file
  defindSize(String? path, int decimals) {
        final file = File(path!);
        int bytes = file.lengthSync();
        log("lengthSync $bytes", name: "FILE SIZE");
        double sizeMb = bytes / (1000 * 1000);
        // the return is mb default
        return sizeMb.toStringAsFixed(decimals);
        // below if you wanted to return suffix just uncomment them
        // const suffixes = ["b", "kb", "mb", "gb", "tb"];
        // var i = (m.log(bytes) / m.log(1024)).floor();
        // return ((bytes / m.pow(1024, i)).toStringAsFixed(decimals)) + suffixes[i];
      }

相关问题