为什么不允许在android中使用?使用react-native-image-picker

问题描述

我有问题。 我已经添加了允许使用“图片”的代码

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

但是, 我有一个如下所示的console.log,

Response =  {error: "Permissions weren't granted"}

为什么我有一个问题,我在/android/src/main/AndroidManifest.xml中添加了允许代码的代码?


在组件的实体代码下方

import ImagePicker from "react-native-image-picker";

...
  const [url,setUrl] = useState(null);
...
  <Button title={"Upload Image"} onPress={() => {
          ImagePicker.showImagePicker(options,(response) => {
            console.log("Response = ",response);

            if (response.didCancel) {
              console.log("User cancelled image picker");
            } else if (response.error) {
              console.log("ImagePicker Error: ",response.error);
            } else if (response.customButton) {
              console.log("User tapped custom button: ",response.customButton);
            } else {
              // const source = { uri: response.uri };

              // You can also display the image using data:
              // const source = { uri: 'data:image/jpeg;base64,' + response.data };
              setUrl("data:image/jpeg;base64," + response.data);
            }
          });
        }}/>
        {url && (
          <Image source={{uri:url}} style={{width:340,height:340}} />
        )}

在android / src / main / AndroidManifest.xml的实体代码下面

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.sampleapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_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|screenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>

</manifest>

解决方法

在选择图片之前,请先征得用户的许可。

const requestCameraPermission = async () => {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.CAMERA,{
        title: "Cool Photo App Camera Permission",message:
          "Cool Photo App needs access to your camera " +
          "so you can take awesome pictures.",buttonNeutral: "Ask Me Later",buttonNegative: "Cancel",buttonPositive: "OK"
      }
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can use the camera");
    } else {
      console.log("Camera permission denied");
    }
  } catch (err) {
    console.warn(err);
  }
};

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...