Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images saved not showing in gallery flutter

Tags:

path

flutter

dart

I have created a function that created folders in my external directory. After that, I am downloading an image from the internet and saving it to the "Gallery Images" folder. The image is being saved in the folder but it is not visible in the galley. Am I missing something?

Code:

  void createFolder() async {
    String directory = (await p.getExternalStorageDirectory()).path;
    List<String> externalPathList = directory.split('/');
    int posOfAndroidDir = externalPathList.indexOf('Android');
    String rootPath = externalPathList.sublist(0, posOfAndroidDir + 1).join('/');
    final path = Directory("$rootPath/Gallery");
    var storageStatus = await Permission.storage.status;
    var externalStorageStatus = await Permission.manageExternalStorage.status;
    if (!storageStatus.isGranted) {
      await Permission.storage.request();
    }
    if (!externalStorageStatus.isGranted) {
      await Permission.manageExternalStorage.request();
    }
    if ((await path.exists())) {
      print("exists");
      print(path.path.toString());
    } else {
      var value = await path.create();
      print("create success");
      print(value.path.toString());
      await Directory(rootPath + "/Gallery" + "/Gallery Images").create(recursive: true);
      await Directory(rootPath + "/Gallery" + "/Gallery Video").create(recursive: true);
      await Directory(rootPath + "/Gallery" + "/Gallery Documents").create(recursive: true);
      await Directory(rootPath + "/Gallery" + "/Gallery Audio").create(recursive: true);
      await Directory(rootPath + "/Gallery" + "/Gallery Voice Notes").create(recursive: true);
      await Directory(rootPath + "/Gallery" + "/.thumbnails").create(recursive: true);
      await Dio().download(url, rootPath + "/Gallery" + "/Gallery Images", onReceiveProgress: (int sent, int total) {
        final progress = (sent / total) * 100;
        print('image download progress: $progress');
       
      });
  
    }
  }
like image 457
ali262883 Avatar asked Nov 25 '25 07:11

ali262883


1 Answers

Whenever you create a new file the device can't automatically find it. You'd have to manually tell the device to refresh the files. Before you'd have to refresh all the files in the device for it to get updated which is very inefficient, but now you could just send the path of the file that you want to get updated. If you're using android then you can use this package to do so.

If you wanna do it yourself with kotlin then here's the code

    private fun broadcastFileUpdate(path: String) {
        context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(File(path))))
        println("updated!")
    }
like image 100
Shaan Mephobic Avatar answered Nov 26 '25 22:11

Shaan Mephobic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!