如何在安卓 – Flutter 中选择任何文件/文件路径。

huangapple go评论61阅读模式
英文:

How to select any file/file path in android - Flutter

问题

I have an app, and I need to choose files like PDF JSON, etc. First, I export my file, and then I can import it. To do this, I use the file_picker package with the below codes. Now the question is, I can pick any file's path in iOS, but on the Android side, I can not select the file or file path directly, even though I give permission in the AndroidManifest.

在iOS上,当我从file_picker触发pickFile()函数时,它正常工作。在Android部分,当我触发该函数时,它会打开文件,但我无法直接选择路径或文件。即使我在AndroidManifest中授予了权限。

Here are the codes that I use:

以下是我使用的代码:

 final result = await FilePicker.platform.pickFiles(
                type: FileType.custom,
                allowedExtensions: ['json'],
              );

Here, after getting the result I check the other conditions. Right now the problem is, as I said, I cannot get the file/file path from the Android side specifically.

在这里,获取结果后,我会检查其他条件。现在的问题是,正如我所说的,我无法从Android端获取文件/文件路径。

Android Manifest section:

Android清单部分:

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

(Note: This is the translated text from the provided content, excluding the code sections.)

英文:

I have an app, and I need to choose files like PDF JSON, etc. First, I export my file, and then I can import it. To do this, I use the file_picker package with the below codes. Now the question is, I can pick any file's path in iOS, but on the Android side, I can not select the file or file path directly, even though I give permission in the AndroidManifest.

in iOS, when I trigger pickFile() function from the file_picker, it works as well. In the Android section, when I trigger the function, it opens the files, but I can not select the path or file directly. Even I can not see Flutter's application folder.

Here are the codes that I use:

 final result = await FilePicker.platform.pickFiles(
                type: FileType.custom,
                allowedExtensions: [&#39;json&#39;],
              );

Here, after getting the result I check the other conditions. Right now problem is, as I said, I can not get the file/file path from the android side specifically.

Android Manifest section:

&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.MANAGE_EXTERNAL_STORAGE&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_MEDIA_LOCATION&quot; /&gt;

答案1

得分: 1

为了限制对共享存储的广泛访问,请针对Android 11(API级别30)或更高版本,通过MANAGE_EXTERNAL_STORAGE权限请求所有文件访问权限。

要在将WRITE_EXTERNAL_STORAGE添加到AndroidManifest.xml后执行此操作,请将[permission_handler][1]包添加到您的应用程序并像这样请求权限:

void requestPermission() {
  await Permission.manageExternalStorage.request();
}
英文:

To limit broad access to shared storage, target Android 11 (API level 30) or higher, request all-files access through the MANAGE_EXTERNAL_STORAGE permission.

To do that after adding WRITE_EXTERNAL_STORAGE to AndroidManifest.xml, add permission_handler package to your apps and Request permission like:

 void requestPermission() {  

await Permission.manageExternalStorage.request();
  
  }

huangapple
  • 本文由 发表于 2023年4月19日 15:20:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定