简体   繁体   English

保存到本地应用程序目录时没有任何反应? 扑

[英]Nothing happens when saving to local app directory? Flutter

I'm picking an image from gallery, and then I should save it to the App Local Directory , But that directory is never created!我从图库中挑选了一张图片,然后我应该将它保存到App Local Directory中,但该目录永远不会创建!

I'm using image_picker and path_provider libraries, and used no permission.我正在使用image_pickerpath_provider库,并且没有使用权限。

Inside Press Button function:内部按键功能:

getImageIntoLocalFiles(ImageSource.gallery); //call getImage function

The 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');
  }

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 com.ziad.TM.time_manager 从未真正创建

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.使用旧版存储管理(共享存储)而不是 Android 10 及更高版本使用的新存储管理(范围存储)。

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".请记住,通过使用“setDestinationInExternalFilesDir”文件将下载到专用于您的应用程序的外部存储器,因此:“external/Android/data/your_app_name/path_you_used_on_function”。

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 .使用ext_storagepath_provider找到要保存的位置的正确路径。 You will need this permission您将需要此权限

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

on Android 10 you need this in your manifest在 Android 10 上,您需要在清单中使用它

<application
      android:requestLegacyExternalStorage="true"

on Android 11 use this instead在 Android 11 上改用这个

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

Remember to ask for them using permission_handler记得使用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);
  }

according to flutter's documentation根据颤振的文档

getApplicationDocumentsDirectory : On Android, this uses the getDataDirectory API on the context. getApplicationDocumentsDirectory :在 Android 上,这在上下文中使用getDataDirectory API。 Consider using getExternalStorageDirectory instead if data is intended to be visible to the user.如果数据旨在对用户可见,请考虑使用getExternalStorageDirectory

So, by changing getApplicationDocumentsDirectory() to getExternalStorageDirectory() the directory is created and the file is successfully saved!!因此,通过将getApplicationDocumentsDirectory()更改为getExternalStorageDirectory()来创建目录并成功保存文件!!


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)并且不要忘记将dir.path传递给saveTo()参数(字符串路径不是目录)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM