Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app restart after select a image with "react-native-image-picker" #7

Open
12343954 opened this issue Jun 13, 2020 · 0 comments
Open

Comments

@12343954
Copy link

12343954 commented Jun 13, 2020

Android 10 , on "SupportScreen", add image-picker code.
after select a image, app navigate to the loading screen. it seems restart.

"react": "16.11.0",
"react-native": "0.62.2",
"react-native-image-picker": "^2.3.1",
import React, { useState, useEffect } from 'react';
import {
  View, Text, Button, StyleSheet, Image,
  PermissionsAndroid, Linking, Alert
} from 'react-native';
import ImagePicker from 'react-native-image-picker';


const SupportScreen = () => {
  const [source, setSource] = useState(null);
  const defaultImage = require('../assets/logo.png');
  const options = {
    title: 'Select Image',
    quality: 0.5,
    maxWidth: 600,
    maxHeight: 500,
    cancelButtonTitle: 'Cancel',
    takePhotoButtonTitle: 'Camera',
    chooseFromLibraryButtonTitle: 'Select from library',
    chooseWhichLibraryTitle: 'Select',
    cameraType: 'back',
    mediaType: 'photo',
    // customButtons: [{ name: 'fb', title: 'Choose Photo from Facebook' }],
    storageOptions: {
      skipBackup: true,
      path: 'images',
    },
    // allowsEditing: false,
    noData: true,
    permissionDenied: {
      title: 'Need permissons',
      text: 'Please authorize the Camera and Photo permissions',
      reTryTitle: 'Go to settings',
      okTitle: 'OK'
    },
  };
  // android permissions
  const _showPicker = (type, option) => {
    PermissionsAndroid.requestMultiple([
      PermissionsAndroid.PERMISSIONS.CAMERA,
      PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE
    ]).then(granted => {
      console.log('global.CheckAndroidPermissions', granted)
      if (granted[PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE] === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN
        || granted[PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE] === PermissionsAndroid.RESULTS.DENIED) {//GRANTED
        Alert.alert(''Need permissons', 'Please authorize the Camera and Photo permissions', [
          {
            text: 'Cancel', style: "cancel", onPress: () => {
              // Linking.openSettings()
            },
          },
          {
            text: 'Go to settings', onPress: () => {
              Linking.openSettings()
            },
          }
        ], { cancelable: false });
        return;
      }
     
      //#region //select a image
      try {
        ImagePicker.showImagePicker(options, (response) => {
          console.log('Response = ', response);
          // return;

          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: `file://${response.path}` };

            // You can also display the image using data:
            // const source = { uri: 'data:image/jpeg;base64,' + response.data };

            // setSource(source)

          }
        });
      } catch (err) {
        console.log(err)
      }
      //#endregion
    })
  }
  return (
    <View style={styles.container}>
      <Text>Support Screen</Text>
      <Button
        title="Click Here"
        onPress={() => _showPicker()}
      />
      <View>
        <Image source={source ? { uri: source } : defaultImage} resizeMode='cover'
          style={{ width: 100, height: 100 }} />
      </View>
    </View>
  );
};

export default SupportScreen;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
  },
});


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant