0

I need to save picture in a specific location rather than in temporary location.

void _takePicture(BuildContext context) async {
    try {
      await _initializeCameraControllerFuture;

      final path =
      join((await getTemporaryDirectory()).path, '${DateTime.now()}.png');

      await _cameraController.takePicture(path);

      Navigator.pop(context,path);

    } catch (e) {
      print(e);
    }
  }
satsin06
  • 1
  • 2
  • 2
    check this out https://stackoverflow.com/questions/51338041/how-to-save-image-file-in-flutter-file-selected-using-image-picker-plugin – Lakmal Fernando Oct 21 '21 at 11:06

1 Answers1

0

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;

Or check this.

G H Prakash
  • 1,720
  • 10
  • 30