【问题标题】:Restrict user of selecting above 1 MB image from gallery or camera using Flutter限制用户使用 Flutter 从图库或相机中选择大于 1 MB 的图像
【发布时间】:2020-12-22 08:37:29
【问题描述】:

我想限制用户选择大于 1 MB 的图像。我通过使用 imagepicker Flutter 包获得了图像路径,但无法获得像 KB 或 MB 这样的图像大小。请帮助某人。

提前致谢 苏罗夫·巴塔查尔吉

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    确保您具有文件读取权限,并且您可以使用以下适用于包括图像在内的所有类型文件的实用程序功能

    checkFileSize(path) {
        var fileSizeLimit = 1024;
        File f = new File(path);
        var s = f.lengthSync()
        print(s); // returns in bytes
        var fileSizeInKB = s / 1024;
        // Convert the KB to MegaBytes (1 MB = 1024 KBytes)
        var fileSizeInMB = fileSizeInKB / 1024;
    
        if(fileSizeInKB > fileSizeLimit) {
           print("File size greater than the limit");
           return false;
        } else {
          print("file can be selected");
          return true;
        }
     }
    

    【讨论】:

      【解决方案2】:

      您可以使用以下函数来获取图像的大小: Im.Image 图像 = Im.decodeImage(imageFile.readAsBytesSync()); 然后把它放在一个简单的 if-else 语句中。

      【讨论】:

        猜你喜欢
        • 2019-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-26
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        相关资源
        最近更新 更多