android 存储许可被拒绝

nhaq1z21  于 9个月前  发布在  Android
关注(0)|答案(1)|浏览(105)

我的应用程序在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)

我所尝试的

我发现了多个相关问题:

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

g2ieeal7

g2ieeal71#

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

<manifest xlmns:android="...">
  <application
    android:label="..."
    android:requestLegacyExternalStorage="true">
    
    <!-- Your manifest stuff here -->

  </application>
</manifest>

字符串

相关问题