Flutter/Dart:将图像文件保存到“/storage/emulated/0/Picture/AppFolder/”

时间:2021-02-28 10:37:14

标签: flutter storage android-storage

使用

final dir = await getExternalStorageDirectory();

图片保存在

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

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

我查遍了所有的 www 却无法弄清楚。请指教。

3 个答案:

答案 0 :(得分:1)

必须先从返回的位置提取根Path
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";
...

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

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

答案 1 :(得分:1)

这里是如何实现这一点,

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

答案 2 :(得分:0)

更新:

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

来自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

相关问题