我的应用程序在 Linux 上运行良好,但我在 Android 的权限系统上遇到了困难。按下按钮,我想加载从另一个程序导出的
json
文件。但是,我收到错误:
I/flutter (24500): [MethodChannelFilePicker] Platform exception: PlatformException(read_external_storage_denied, User did not allow reading external storage, null, null)
E/flutter (24500): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(read_external_storage_denied, User did not allow reading external storage, null, null)
我觉得我不需要所有这些权限,但我只是尝试更多权限是否可以解决我的问题。
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The INTERNET permission is required for development. Specifically,
the Flutter tool needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
</manifest>
Future<void> requestPermission() async {
var status = await Permission.storage.status;
if (!status.isGranted) {
print("Requesting Permission");
status = await Permission.storage.request();
print("Again? $status");
// You might want to check status again here and handle denied permissions accordingly
} else {
print("Not requesting Permission!");
}
}
状态返回,访问被拒绝并尝试访问文件:
Future<void> pickFile(parent) async {
await requestPermission();
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['json'],
);
if (result != null) {
String filePath = result.files.single.path!; // Assuming the file path is not null
try {
File file = File(filePath);
if (await file.exists()) {
String fileContent = await file.readAsString();
// Parse the JSON content
Map<String, dynamic> jsonData = jsonDecode(fileContent);
} else {
print('File does not exist');
}
} catch (e) {
print('Error reading file: $e');
}
}
}
失败:
I/flutter (24500): [MethodChannelFilePicker] Platform exception: PlatformException(read_external_storage_denied, User did not allow reading external storage, null, null)
E/flutter (24500): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(read_external_storage_denied, User did not allow reading external storage, null, null)
我发现了多个相关问题:
但是要求提供照片、视频或音频并不符合我的需求。 “将你的 targetSdkVersion 降级到 32”没有帮助,因为 GooglePlay 要求 >=33 (但降级到 32 确实有效)。
您是否尝试将其添加到应用程序标签中
android:requestLegacyExternalStorage =“true