我想在从相机获取文件后将图像保存在图库中,如何创建一个新目录并保存我们从相机获取的图像文件?
Future getImageCamera() async {
var imageFile = await ImagePicker.pickImage(
source: ImageSource.camera, maxHeight: 20.0, maxWidth: 20.0);
DateTime ketF = new DateTime.now();
String baru = "${ketF.year}${ketF.month}${ketF.day}";
//proses kompres
final temDirk = await getTemporaryDirectory();
final pathss = temDirk.path;
int rand = new math.Random().nextInt(100000);
//mengganti nama file
// String juduld = controllerJudul.text;
img.Image gambard = img.decodeImage(imageFile.readAsBytesSync());
img.Image gambarKecilx = img.copyResize(gambard,
700); //parameter awal adalah sumber gambar // parameter kedua ukuran width, hp=>1k
var kompresimg = new File("$pathss/image_$baru$rand.jpg")
..writeAsBytesSync(img.encodeJpg(gambarKecilx, quality: 95));
setState(() {
_gambar = kompresimg;
namafile = "image_$baru$rand.jpg";
});
您需要将图像保存到外部存储目录中,以便在图库上显示图像。
不获取临时目录,而是获取外部存储目录。
final directory = await getExternalStorageDirectory();
您需要提供以下权限AndroidManifest.xml
你的文件android/app/src/main
folder
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
然后假设您要创建一个名为 MyImages 的文件夹并将新图像添加到该文件夹中,
final myImagePath = '${directory.path}/MyImages' ;
final myImgDir = await new Directory(myImagePath).create();
然后将文件写入该路径。
var kompresimg = new File("$myImagePath/image_$baru$rand.jpg")
..writeAsBytesSync(img.encodeJpg(gambarKecilx, quality: 95));
要获取文件数量,只需将文件获取到列表并检查列表的长度
var listOfFiles = await myImgDir.list(recursive: true).toList();
var count = countList.length;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)