保存到本地应用程序目录时什么都没发生?扑
我正在从画廊中挑选图像,然后我应该将其保存到 app local Directory ,但是该目录是永远不会创建的!
我正在使用image_picker
和path_provider
库,并且不使用许可。
内部按钮函数:
getImageIntoLocalFiles(ImageSource.gallery); //call getImage function
getImage函数:
Future getImageIntoLocalFiles(ImageSource imageSource) async {
final ImagePicker _picker = ImagePicker();
// using your method of getting an image
final XFile? image = await _picker.pickImage(source: imageSource);
//debug
print('${image?.path} ---image path'); //returns /data/user/0/com.ziad.TM.time_manager/cache/image_picker6496178102967914460.jpg
// getting a directory path for saving
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
//debug
if(image != null) print('$appDocPath ---local directory path'); //return /data/user/0/com.ziad.TM.time_manager/app_flutter
// copy the file to a new path
await image?.saveTo('$appDocPath/image1.png');
}
我不明白问题在哪里,因为没有错误,我找不到解释这一点的文档。
com.ziad.tm.time_manager从未真正创建
I'm picking an image from gallery, and then I should save it to the App Local Directory, But that directory is never created!
I'm using image_picker
and path_provider
libraries, and used no permission.
Inside Press Button function:
getImageIntoLocalFiles(ImageSource.gallery); //call getImage function
The getImage function:
Future getImageIntoLocalFiles(ImageSource imageSource) async {
final ImagePicker _picker = ImagePicker();
// using your method of getting an image
final XFile? image = await _picker.pickImage(source: imageSource);
//debug
print('${image?.path} ---image path'); //returns /data/user/0/com.ziad.TM.time_manager/cache/image_picker6496178102967914460.jpg
// getting a directory path for saving
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
//debug
if(image != null) print('$appDocPath ---local directory path'); //return /data/user/0/com.ziad.TM.time_manager/app_flutter
// copy the file to a new path
await image?.saveTo('$appDocPath/image1.png');
}
I don't understand where is the problem as there is no errors and I can't find a documentation explaining this.
com.ziad.TM.time_manager is never actually created
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要保存文件,请按照下面的说明进行操作。
通过使用 ext_storage href =“ href =“ htttps:htttps: //pub.dev/packages/path_provider#-readme-tab-“ rel =” nofollow noreferrer“> path_provider 。您需要在Android 10上获得此权限,
清单中使用此权限
您需要在Android 11上的
,而请记住使用许可证_handler
和存储文件,请致电下面的方法
To save the files, please follow below instructions.
find the correct path of the location you want to save by using ext_storage or path_provider. You will need this permission
on Android 10 you need this in your manifest
on Android 11 use this instead
Remember to ask for them using permission_handler
And to store file, please call below method
根据Flutter的文档
因此,通过更改
getApplicationDocumentsDirectory()
togetExternalStoreDirectory()
创建了目录,并且文件已成功保存!我也找到了一种在内部创建新的自定义文件夹的方法:
,不要忘记将
dir.path.path
saveto()
parameter(字符串路径而不是目录)according to flutter's documentation
So, by changing
getApplicationDocumentsDirectory()
togetExternalStorageDirectory()
the directory is created and the file is successfully saved!!I also found a way to create a new custom folder inside:
and don't forget to pass
dir.path
tosaveTo()
parameter (a string path not a Directory)解决:
我仅通过使用以下方式
我的相对路径是:
我的清单中也有:
使用旧版存储管理(共享存储),而不是新的存储管理(Scoped Storage)(Scoped Storage)。
请记住,通过使用“ setDestinationinexternalfilesdir”文件将下载到专用于您的应用程序的外部内存中,因此:“外部/android/data/your_app_name/path_you_used_oned_on_function”。
I solved just by using:
Instead of:
My relative path is:
I also have in my manifest:
To use Legacy storage management (Shared Storage) instead of new storage management (Scoped Storage) used from Android 10 and above.
Remember that by using "setDestinationInExternalFilesDir" files will be download to the external memory dedicated to your app, so: "external/Android/data/your_app_name/path_you_used_on_function".