Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sbin0819/daa7bd109f37de09f5a73174e34eee87 to your computer and use it in GitHub Desktop.
Save sbin0819/daa7bd109f37de09f5a73174e34eee87 to your computer and use it in GitHub Desktop.
import {Platform, PermissionsAndroid} from 'react-native';
import {
  launchCamera,
  launchImageLibrary,
  ImagePickerResponse,
} from 'react-native-image-picker';

export const onHandleCamera = async () => {
  if (Platform.OS === 'android') {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.CAMERA,
        {
          title: 'App Camera Permission',
          message: 'App needs access to your camera ',
          buttonNeutral: 'Ask Me Later',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        },
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        launchCamera(
          {
            mediaType: 'photo',
            saveToPhotos: true,
          },
          (res: ImagePickerResponse) => {
            console.log(res);
          },
        );
      } else {
        console.log('Camera permission denied');
      }
    } catch (err) {
      console.warn(err);
    }
  } else {
    launchCamera(
      {
        mediaType: 'photo',
        saveToPhotos: true,
      },
      (res: ImagePickerResponse) => {
        console.log(res);
      },
    );
  }
};

export const onSelectImage = async () => {
  await launchImageLibrary(
    {
      mediaType: 'photo',
      maxHeight: 512,
      maxWidth: 512,
      includeBase64: Platform.OS === 'android',
      selectionLimit: 14, // 멀티 이미지 셀렉트 14 가 한계
    },
    (response: any) => {
      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 {
        console.log(response);
      }
    },
  );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment