【问题标题】:Flutter/Dart: Saving an image file to "/storage/emulated/0/Picture/AppFolder/"Flutter/Dart:将图像文件保存到“/storage/emulated/0/Picture/AppFolder/”
【发布时间】:2021-05-30 03:36:29
【问题描述】:

使用

final dir = await getExternalStorageDirectory();

图像保存在

/storage/emulated/0/Android/data/com.example.flutter_app/files/

但我想将它存储在../0/Pictures/app_name/ 中,以便它显示在图库中。

我翻遍了 www 却想不通。请指教。

【问题讨论】:

    标签: flutter storage android-storage


    【解决方案1】:

    您必须先从返回的位置提取根路径
    rootPath = /storage/emulated/0/

    而不是创建Picturesapp_name 目录(以避免目录不存在时出现异常) 然后将文件保存在/storage/emulated/0/Pictures/app_name/

    这里有一个简单的例子可以帮助你理解:

    ...
    
        Directory externalPath = (await getExternalStorageDirectory());
        String picturesDirName = "Pictures";
        String appNameDirName = "app_name";
        
    
    // Splitting the externalPath
        List<String> externalPathList = externalPath.path.split('/');
    
    
    // getting Position of 'Android'
        int posOfAndroidDir = externalPathList.indexOf('Android');
    
    //Joining the List<Strings> to generate the rootPath with "/" at the end.
        String rootPath = externalPathList.sublist(0, posOfAndroidDir).join('/');
        rootPath+="/";
    
    //Creating Pictures Directory (if not exist)
        Directory picturesDir = Directory(rootPath+picturesDirName+"/");
        if (!picturesDir.existsSync()) {
          //Creating Directory
          await picturesDir.create(recursive: true);
          //Directory Created
        } else {
          //Directory Already Existed
        }
    
    //Creating "app_name" Directory (if not exist)
        Directory appNameDir = Directory(rootPath+picturesDirName+"/"+appNameDirName+"/");
        if (!appNameDir.existsSync()) {
          //Creating Directory
          await appNameDir.create(recursive: true);
          //Directory Created
        } else {
          //Directory Already Existed
        }
        
    //Creating String varible to store the path where you want to save file.
        String fileSaveLocation = rootPath+picturesDirName+"/"+appNameDirName+"/";
    // Or you can also use templates like this
        String fileSaveLocation2 = "$rootPath$picturesDirName/$appNameDirName/";
        
    //Your File Path where you want to save you file.
        String filePath = fileSaveLocation+"text.txt";
    // Or you can also use templates like this
        String filePath2 = "${fileSaveLocation2}test.txt";
    ...
    

    您可以根据自己的喜好优化上述代码。

    希望这是您正在寻找的解决方案。

    【讨论】:

    • 感谢您提供非常详细的回答。这会有所帮助。
    【解决方案2】:

    在这里,您可以如何实现这一目标,

    final Directory extDir = await getExternalStorageDirectory();
    String dirPath = '${extDir.path}/app_name/pictures';
    dirPath = dirPath.replaceAll("Android/data/com.example.flutter_app/files/", "");
    await Directory(dirPath).create(recursive: true);
    // start File Operations Here with dirPath variable
    

    【讨论】:

    • 这非常精确。不过,我需要在第二行将添加的字符串切换到/Pictures/app_name/,以便生成/storage/emulated/0/Pictures/app_name/。谢谢。
    • 如果这解决了您的问题,那么您可以关闭问题
    【解决方案3】:

    更新:

    除了下面的详细答案外,我还发现了几个专门处理媒体内容的插件。

    来自Flutter.dev docs

    插件目前支持访问两个文件系统位置:

    1. gallery_saver Plugin: See Full Example
    GallerySaver.saveImage(path).then((bool success) {// code }
    
    1. Image_gallery_saver: See Full Example
    await ImageGallerySaver.saveImage(Uint8List.fromList(response.data), quality: 60, name: "hello");
    
    1. 手动创建路径:Source
    await Permission.storage.request();
    if (await Permission.storage.isGranted) {
      var dir = await getExternalStorageDirectory();
      Dio dio = Dio();
      var newPath = "";
      List<String> paths = dir.path.split("/");
      for (int x = 1; x < paths.length; x++) {
          String folder = paths[x];
          if (folder != "Android") {
              newPath += "/" + folder;
            } 
          else {
              break;
          }
      }
      var picfolder = "/Pictures/";
      newPath = newPath + picfolder + AppStrings['AppName'];
    

    其他有用的参考资料:
    Android Data Storage
    MediaStore in Flutter

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多