无法请求 React Native Android 应用程序 AUG 2024 的权限

问题描述 投票:0回答:1

我正在构建一个反应本机应用程序。我正在运行 Pixel 7 虚拟设备,我正在尝试请求访问手机文件系统的权限以从应用程序下载图像,但是我没有看到请求窗口弹出,并且权限设置为 never_ask_afain,即使我从来没有将其设置为这个。我已经重新安装了应用程序,尝试了不同的虚拟设备等等

const requestStoragePermission = async () => {
    if (Platform.OS === 'android') {
      try {
        console.log(Platform.OS);
        const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
            {
              title: 'Storage Permission',
              message: 'App needs access to your storage to save the QR code',
              buttonNeutral: 'Ask Me Later',
              buttonNegative: 'Cancel',
              buttonPositive: 'OK',
            },
        );
        console.log(`Results... ${granted}`)
        return granted === PermissionsAndroid.RESULTS.GRANTED;
      } catch (err) {
        console.warn(err);
        return false;
      }
    } else {
      return true; // iOS does not need this permission
    }
  };

这是我的日志的输出

 android
 LOG  Results... never_ask_again

这是我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:theme="@style/AppTheme">
    <activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize" android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="inethi"/>
      </intent-filter>
    </activity>
  </application>
</manifest>

我的 package.json 文件:

{
  "name": "inethi",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "lint": "eslint .",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "@react-keycloak/native": "^0.6.4",
    "@react-native-async-storage/async-storage": "^1.23.1",
    "@react-native-camera-roll/camera-roll": "^5.6.0",
    "@react-native-clipboard/clipboard": "^1.14.1",
    "@react-native-picker/picker": "^2.7.5",
    "axios": "^1.6.8",
    "qrcode": "^1.5.4",
    "react": "18.2.0",
    "react-native": "0.73.4",
    "react-native-blob-util": "^0.19.11",
    "react-native-document-picker": "^9.1.1",
    "react-native-fs": "^2.20.0",
    "react-native-images-to-pdf": "^0.2.1",
    "react-native-paper": "^5.12.3",
    "react-native-pdf": "^6.7.5",
    "react-native-permissions": "^4.1.5",
    "react-native-qrcode-svg": "^6.3.2",
    "react-native-safe-area-context": "^4.9.0",
    "react-native-svg": "^15.5.0",
    "react-native-vector-icons": "^10.0.3",
    "react-native-webview": "^13.8.1",
    "react-router-native": "^6.22.1",
    "rn-fetch-blob": "^0.12.0"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native/babel-preset": "0.73.21",
    "@react-native/eslint-config": "0.73.2",
    "@react-native/metro-config": "0.73.5",
    "@react-native/typescript-config": "0.73.1",
    "@types/react": "^18.2.6",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.6.3",
    "eslint": "^8.19.0",
    "jest": "^29.6.3",
    "prettier": "2.8.8",
    "react-test-renderer": "18.2.0",
    "typescript": "5.0.4"
  },
  "engines": {
    "node": ">=18"
  }
}

我正在使用React Native v0.73.4。

我希望出现权限窗口并询问用户是否会授予权限,但它永远不会这样做

android react-native permissions
1个回答
0
投票

解决步骤: 请求前检查权限:

确保您在请求之前检查权限状态。如果权限已设置为 never_ask_again,您可能需要将用户引导至应用设置以手动启用它。

修改您的代码以处理 never_ask_again:

清除应用程序数据或重置权限:

如果您一直遇到 never_ask_again 状态,请尝试清除应用程序数据或通过设备设置重置应用程序权限。

© www.soinside.com 2019 - 2024. All rights reserved.