flutter - 保存的图像不会在图库中显示

标签 flutter dart path

我创建了一个在我的外部目录中创建文件夹的函数。之后,我从互联网上下载一张图片并将其保存到“图库图片”文件夹中。图像正在保存在文件夹中,但在厨房中不可见。我错过了什么吗?

代码:

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

最佳答案

每当您创建新文件时,设备都无法自动找到它。您必须手动告诉设备刷新文件。之前你必须刷新设备中的所有文件才能更新,这是非常低效的,但现在你可以只发送你想要更新的文件的路径。如果您使用的是 android,那么您可以使用这个 package这样做。

如果你想用 kotlin 自己做,那么这里是代码

    private fun broadcastFileUpdate(path: String) {
        context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(File(path))))
        println("updated!")
    }

关于flutter - 保存的图像不会在图库中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69016968/

相关文章:

Python:获取给定文件夹中特定文件类型的名称

path - conda :激活环境后会发生什么?

flutter - 如何处理在 flutter 中动态创建的复选框列表?

c++ - 在 Flutter Windows 桌面应用程序中使用 C++ DLL

flutter - Flutter中离开页面后停止播放音乐

tabs - Flutter - 使用带有动态选项卡 View 的 TabBarView 小部件实现抽屉导航

redux - 参数列表不能分配给参数类型列表

java - 安装和使用 Emscripten

android - 使用 Flutter 的 Twilio 视频

flutter - 空安全 AppLocalization 字符串的最佳方法