android 存储许可被拒绝

nhaq1z21  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(159)

我的应用程序在Linux上运行良好,但我与android的权限系统斗争。按下按钮,我想加载从另一个程序导出的json文件。然而,我得到错误:

  1. I/flutter (24500): [MethodChannelFilePicker] Platform exception: PlatformException(read_external_storage_denied, User did not allow reading external storage, null, null)
  2. 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)

字符串

设置

清单

我觉得我不需要所有这些权限,但我只是尝试了更多的权限是否可以解决我的问题。

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android">
  2. <!-- The INTERNET permission is required for development. Specifically,
  3. the Flutter tool needs it to communicate with the running application
  4. to allow setting breakpoints, to provide hot reload, etc.
  5. -->
  6. <uses-permission android:name="android.permission.INTERNET"/>
  7. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  8. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  9. <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
  10. </manifest>

授予权限代码

  1. Future<void> requestPermission() async {
  2. var status = await Permission.storage.status;
  3. if (!status.isGranted) {
  4. print("Requesting Permission");
  5. status = await Permission.storage.request();
  6. print("Again? $status");
  7. // You might want to check status again here and handle denied permissions accordingly
  8. } else {
  9. print("Not requesting Permission!");
  10. }
  11. }


状态返回,访问被拒绝,正在尝试访问文件:

  1. Future<void> pickFile(parent) async {
  2. await requestPermission();
  3. FilePickerResult? result = await FilePicker.platform.pickFiles(
  4. type: FileType.custom,
  5. allowedExtensions: ['json'],
  6. );
  7. if (result != null) {
  8. String filePath = result.files.single.path!; // Assuming the file path is not null
  9. try {
  10. File file = File(filePath);
  11. if (await file.exists()) {
  12. String fileContent = await file.readAsString();
  13. // Parse the JSON content
  14. Map<String, dynamic> jsonData = jsonDecode(fileContent);
  15. } else {
  16. print('File does not exist');
  17. }
  18. } catch (e) {
  19. print('Error reading file: $e');
  20. }
  21. }
  22. }


失败原因:

  1. I/flutter (24500): [MethodChannelFilePicker] Platform exception: PlatformException(read_external_storage_denied, User did not allow reading external storage, null, null)
  2. 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)

我所尝试的

我发现了多个相关问题:

但是要求照片、视频或音频并不符合我的需要。“将你的目标SdkVersion降级到32”没有帮助,因为GooglePlay需要>=33(但是降级到32确实有效)。

g2ieeal7

g2ieeal71#

尝试在AndroidManifest.xml文件中将其添加到application

  1. <manifest xlmns:android="...">
  2. <application
  3. android:label="..."
  4. android:requestLegacyExternalStorage="true">
  5. <!-- Your manifest stuff here -->
  6. </application>
  7. </manifest>

字符串

相关问题