保存到本地应用程序目录时什么都没发生?扑

发布于 2025-02-09 14:39:46 字数 1203 浏览 1 评论 0原文

我正在从画廊中挑选图像,然后我应该将其保存到 app local Directory ,但是该目录是永远不会创建的!

我正在使用image_pickerpath_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫性小仙女 2025-02-16 14:39:46

要保存文件,请按照下面的说明进行操作。

通过使用 ext_storage href =“ href =“ htttps:htttps: //pub.dev/packages/path_provider#-readme-tab-“ rel =” nofollow noreferrer“> path_provider 。您需要在Android 10上获得此权限,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

清单中使用此权限

<application
      android:requestLegacyExternalStorage="true"

您需要在Android 11上的

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

,而请记住使用许可证_handler

和存储文件,请致电下面的方法

static Future saveInStorage(
      String fileName, File file, String extension) async {
    await _checkPermission();
    String _localPath = (await ExtStorage.getExternalStoragePublicDirectory(
        ExtStorage.DIRECTORY_DOWNLOADS))!;
    String filePath =
        _localPath + "/" + fileName.trim() + "_" + Uuid().v4() + extension;

    File fileDef = File(filePath);
    await fileDef.create(recursive: true);
    Uint8List bytes = await file.readAsBytes();
    await fileDef.writeAsBytes(bytes);
  }

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

on Android 10 you need this in your manifest

<application
      android:requestLegacyExternalStorage="true"

on Android 11 use this instead

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

Remember to ask for them using permission_handler

And to store file, please call below method

static Future saveInStorage(
      String fileName, File file, String extension) async {
    await _checkPermission();
    String _localPath = (await ExtStorage.getExternalStoragePublicDirectory(
        ExtStorage.DIRECTORY_DOWNLOADS))!;
    String filePath =
        _localPath + "/" + fileName.trim() + "_" + Uuid().v4() + extension;

    File fileDef = File(filePath);
    await fileDef.create(recursive: true);
    Uint8List bytes = await file.readAsBytes();
    await fileDef.writeAsBytes(bytes);
  }
£烟消云散 2025-02-16 14:39:46

根据Flutter的文档

getApplicationDocumentsDirectory:在Android上,这使用了
getDatadirectory api在上下文上。考虑使用
getExternalStoragedirectory如果数据打算为可见
给用户。

因此,通过更改getApplicationDocumentsDirectory() to getExternalStoreDirectory()创建了目录,并且文件已成功保存!


我也找到了一种在内部创建新的自定义文件夹的方法:

String dirToBeCreated = "<New Folder Name>";

    if(baseDir != null) {
      String finalDir = join(baseDir.path, dirToBeCreated);
      var dir = Directory(finalDir);

      bool dirExists = await dir.exists();
      if (!dirExists) {
        dir.create(
            /*recursive=true*/); //pass recursive as true if directory is recursive
      }

,不要忘记将dir.path.path saveto() parameter(字符串路径而不是目录)

according to flutter's documentation

getApplicationDocumentsDirectory : On Android, this uses the
getDataDirectory API on the context. Consider using
getExternalStorageDirectory instead if data is intended to be visible
to the user.

So, by changing getApplicationDocumentsDirectory() to getExternalStorageDirectory() the directory is created and the file is successfully saved!!


I also found a way to create a new custom folder inside:

String dirToBeCreated = "<New Folder Name>";

    if(baseDir != null) {
      String finalDir = join(baseDir.path, dirToBeCreated);
      var dir = Directory(finalDir);

      bool dirExists = await dir.exists();
      if (!dirExists) {
        dir.create(
            /*recursive=true*/); //pass recursive as true if directory is recursive
      }

and don't forget to pass dir.path to saveTo() parameter (a string path not a Directory)

傲影 2025-02-16 14:39:46

解决:

setDestinationInExternalFilesDir(context, relativePath, filename);

我仅通过使用以下方式

setDestinationInExternalPublicDir(relativePath, filename);

我的相对路径是:

Environment.getExternalStorageDirectory().getPath() + "MyExternalStorageAppPath"

我的清单中也有:

android:requestLegacyExternalStorage="true"

使用旧版存储管理(共享存储),而不是新的存储管理(Scoped Storage)(Scoped Storage)。

请记住,通过使用“ setDestinationinexternalfilesdir”文件将下载到专用于您的应用程序的外部内存中,因此:“外部/android/data/your_app_name/path_you_used_oned_on_function”。

I solved just by using:

setDestinationInExternalFilesDir(context, relativePath, filename);

Instead of:

setDestinationInExternalPublicDir(relativePath, filename);

My relative path is:

Environment.getExternalStorageDirectory().getPath() + "MyExternalStorageAppPath"

I also have in my manifest:

android:requestLegacyExternalStorage="true"

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文