我正在尝试为我的 React Native 应用程序实现应用程序权限,并且我正在正确遵循 docs。
我的目标:我想请求用户的 WRITE_EXTERNAL_STORAGE 权限。
调用下面的函数时,授予的值始终为“never_ask_again”,并且权限对话根本没有打开。
import { PermissionsAndroid } from 'react-native';
const requestWriteExternalStoragePermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Allow to write files.',
message: 'This app need write permission in order to manage temporary files downloads.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('rest of logic...');
} else {
console.log('WRITE_EXTERNAL_STORAGE permission denied');
}
} catch (err) {
console.warn(err);
}
};
AndroidManifest.xml 如下所示。
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_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>
</activity>
</application>
</manifest>
还在应用程序设置中权限被阻止。 在此输入图片描述
我也尝试卸载应用程序并重置缓存。还尝试了 ./gradlew clean,但效果不佳。有人可以帮助解决这个问题吗?非常感谢。
你能具体告诉我你什么时候调用这个函数的吗? 另外请检查您是否正确导入 android 权限,因为有时如果我们从文档中复制粘贴代码,则它无法正确导入 这是我的代码,可以正常工作,检查一下 我不知道我无法输入代码,请参考图片
请清除存储/数据,然后尝试此代码并在单击某个按钮时调用此函数以获得准确的结果