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

后端 未结 1 1739
别那么骄傲
别那么骄傲 2021-01-12 01:19

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?

相关标签:
1条回答
  • 2021-01-12 02:02

    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;
    
    0 讨论(0)
提交回复
热议问题