在颤动中检查图像大小(kb,mb ...)?

Abb*_*ari 3 image dart flutter

我知道如何检查图像的宽度和高度:

import 'dart:io';

File image = new File('image.png'); // Or any other way to get a File instance.
var decodedImage = await decodeImageFromList(image.readAsBytesSync());
print(decodedImage.width);
print(decodedImage.height)
Run Code Online (Sandbox Code Playgroud)

但是我想检查图像大小,例如 100kb、200kb 或类似的大小,有什么办法吗,请帮助我。

Web*_*ios 13

这是一个使用函数的解决方案,该函数将为您提供文件大小作为整洁的格式化字符串。

进口:

import 'dart:io';
import 'dart:math';
Run Code Online (Sandbox Code Playgroud)

输出:

File image = new File('image.png');

print(getFilesizeString(bytes: image.lengthSync()}); // Output: 17kb, 30mb, 7gb
Run Code Online (Sandbox Code Playgroud)

功能:

// Format File Size
static String getFileSizeString({@required int bytes, int decimals = 0}) {
  const suffixes = ["b", "kb", "mb", "gb", "tb"];
  var i = (log(bytes) / log(1024)).floor();
  return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) + suffixes[i];
}
Run Code Online (Sandbox Code Playgroud)


iDe*_*ode 10

使用lengthInBytes.

final bytes = image.readAsBytesSync().lengthInBytes;
final kb = bytes / 1024;
final mb = kb / 1024;
Run Code Online (Sandbox Code Playgroud)

如果您愿意async-await,请使用

final bytes = (await image.readAsBytes()).lengthInBytes;
Run Code Online (Sandbox Code Playgroud)