ali262883
ali262883

Reputation: 369

Images saved not showing in gallery flutter

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');
       
      });
  
    }
  }

Upvotes: 3

Views: 1343

Answers (1)

Shaan Mephobic
Shaan Mephobic

Reputation: 1216

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!")
    }

Upvotes: 3

Related Questions