英文:
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: ['json'],
);
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:
<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" />
答案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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论