Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter How to save Image file to new folder in gallery?

Tags:

image

flutter

I want to save the image in the gallery after I get the file from the camera, how to create a new directory and save the image file that we have obtained from the camera?

 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";
});
like image 937
Dwi Nur Rohman Avatar asked Feb 07 '19 02:02

Dwi Nur Rohman


People also ask

How do I save an image in a folder in flutter?

// using your method of getting an image final File image = await ImagePicker. pickImage(source: imageSource); // getting a directory path for saving final String path = await getApplicationDocumentsDirectory(). path; // copy the file to a new path final File newImage = await image. copy('$path/image1.

How do I store images in external storage in flutter?

You need to save the image into external storage directory for showing the image on gallery. Instead of getting temporary directory, obtain external storage directory. then let's say that you want to create a folder named MyImages and add the new image to that folder, final myImagePath = '${directory.


1 Answers

You need to save the image into external storage directory for showing the image on gallery. Instead of getting temporary directory, obtain external storage directory.

final directory = await getExternalStorageDirectory();

You need to provide the permission on AndroidManifest.xml file of your android/app/src/main folder

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

then let's say that you want to create a folder named MyImages and add the new image to that folder,

final myImagePath = '${directory.path}/MyImages' ;
final myImgDir = await new Directory(myImagePath).create();

then write to the file to the path.

var kompresimg = new File("$myImagePath/image_$baru$rand.jpg")
  ..writeAsBytesSync(img.encodeJpg(gambarKecilx, quality: 95));

for getting the number of files, just obtain the files to a list and check the length of the list

var listOfFiles = await myImgDir.list(recursive: true).toList();
var count = countList.length;
like image 74
krishnakumarcn Avatar answered Oct 25 '22 02:10

krishnakumarcn