简体   繁体   English

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

[英]Why does not permitted in android ? using react-native-image-picker

i have a problem.我有个问题。 i had added codes for permitting about 'image'我添加了允许“图像”的代码

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

but, i had a console.log like below,但是,我有一个如下所示的 console.log,

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

Why i had a problem that i had added codes for permitting code to /android/src/main/AndroidManifest.xml?为什么我在 /android/src/main/AndroidManifest.xml 中添加了允许代码的代码?


below the entity code of the component在组件的实体代码下方

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}} />
        )}

below the entity code of android/src/main/AndroidManifest.xml在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>

before picking image, ask permission to user..在选择图像之前,请征得用户许可..

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);
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM