簡體   English   中英

File_Picker 插件不返回 Flutter 中寫入文件的絕對路徑

[英]File_Picker plugin doesn't return absolute path for write file in Flutter

我正在使用 file_picker 插件從我的 flutter 應用程序中的設備存儲中挑選文件。 我需要一個絕對路徑,以便我可以讀/寫文件。 但是使用file_picker插件選擇文件只返回存儲在應用程序緩存中的復制文件的路徑(文件加載和緩存在:/data/user/0/com.example.file_locker/cache/file_picker/Screenshot_2021-03-09 -00-59-58-834_com.linkedin.android.jpg)。

我正在使用file_picker: ^2.1.6

file_picker 存儲庫問題 在這里

現在,是否有任何解決方案可以從 file_picker 獲取絕對路徑,以便我可以讀/寫文件? 我被困在我的項目上,您的解決方案將不勝感激。

我見過其他文件資源管理器插件,如果您知道其他返回所需解決方案的其他插件,它們也采用相同的方式,請告訴我。

先感謝您。

在插件頁面的示例中,pickFile 的路徑在 path 屬性中為 state :

FilePickerResult result = await FilePicker.platform.pickFiles();

if(result != null) {
   PlatformFile file = result.files.first;
   
   print(file.name);
   print(file.bytes);
   print(file.size);
   print(file.extension);
   print(file.path);
}

如果您的目標是將 pickFile 寫入應用程序文檔目錄或臨時目錄中的文件,則可以使用 path_provider: https://pub.dev/packages/path_provider

// Get the application document directory
Directory appDocDir = await getApplicationDocumentsDirectory();
// Get the absolute path
String appDocPath = appDocDir.path;
// Copy it to the new file
final File newFile = await file.copy('$appDocPath/your_file_name.${file.extension}');
await FilePicker.platform.getDirectoryPath();

我也遇到過這個問題,問題是新版本的 file_picker 沒有提供絕對路徑。 您可以使用舊版本的 file_picker package。 這可以正常工作,並通過使用以下函數為您提供絕對路徑:

  1. FilePicker.getMultiFilePath();
  2. FilePicker.getFilePath();

(谷歌這些以獲取更多信息。)

您需要在 flutter 中禁用 null 安全性才能使用這些較低版本的 package。

我也遇到過這個問題, file_picker沒有顯示 android 的實際路徑,所以我找到了這種情況的解決方案:“選擇文件並顯示實際路徑”。 我使用這個filesystem_picker按目錄打開文件

         // for android Directory 
         Directory appDocDir = await Directory("storage/emulated/0");

         var result = await FilesystemPicker.open(allowedExtensions: [".png", ".jpg"],
         context: context,rootDirectory: appDocDir);
         if (result != null) {
             File file = File(result);
             print(file.parent.path)// the path where the file is saved 
         }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM