Flutter/Dart:将图像文件保存到“/storage/emulated/0/Picture/AppFolder/”
Posted
技术标签:
【中文标题】Flutter/Dart:将图像文件保存到“/storage/emulated/0/Picture/AppFolder/”【英文标题】:Flutter/Dart: Saving an image file to "/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 却想不通。请指教。
【问题讨论】:
【参考方案1】:您必须先从返回的位置提取根路径 rootPath
= /storage/emulated/0/
而不是创建Pictures
和app_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 = "$fileSaveLocation2test.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:
插件目前支持访问两个文件系统位置:
-
gallery_saver Plugin: See Full Example
GallerySaver.saveImage(path).then((bool success) // code
-
Image_gallery_saver: See Full Example
await ImageGallerySaver.saveImage(Uint8List.fromList(response.data), quality: 60, name: "hello");
-
手动创建路径: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 StorageMediaStore in Flutter
【讨论】:
SO 编辑器中的一个奇怪错误,我无法再添加一个链接。这是:***.com/questions/49797617/…以上是关于Flutter/Dart:将图像文件保存到“/storage/emulated/0/Picture/AppFolder/”的主要内容,如果未能解决你的问题,请参考以下文章
Flutter/Dart - 如何在其他屏幕上的图像选择器后更新图像
如何在视频/图像上添加图像或文本(水印) - Flutter/Dart