question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get permissions doesn't trigger callback

See original GitHub issue

Description

On Android 23+ with React Native 0.43+ (targetSDKVersion 23), the very first time you try to take a picture ou to select one from the library, the permission popups (WRITE_EXTERNAL_STORAGE and PERMISSIONS.CAMERA) shows up, but the callback is never triggered. You have to recall the method to get it working

How to repeat issue and example

    gotPhotoResponse(response:{didCancel:boolean,error:{}}) {
        if (response.didCancel) {
            this.props.onCancel();
        }
        else if (response.error) {
            Alert.alert(
                I18n.t('issue_creation_take_picture_error_title'),
                I18n.t('issue_creation_take_picture_error_message'),
                [
                    {
                        text: I18n.t('issue_creation_take_picture_error_settings'),
                        onPress: this.goToSettings
                    },
                    {
                        text: I18n.t('text_cancel'),
                        onPress: this.props.onCancel
                    }
                ]
            );
        }
        else {
            this.props.onSelect(response);
        }
    }

    selectTakePhotoTapped() {
       ImagePicker.launchCamera(options, this.gotPhotoResponse.bind(this));
    }

    selectPickPhotoTapped() {
        ImagePicker.launchImageLibrary(options, this.gotPhotoResponse.bind(this));
    }

    render() {
        return <View style={styles.container}>
            <View style={styles.bigButton}>
                <Button onPress={this.selectTakePhotoTapped} title={I18n.t('issue_creation_take_picture')}/>
            </View>
            <View style={styles.smallButton}>
                <Button onPress={this.selectPickPhotoTapped}
                        title={I18n.t('issue_creation_select_picture')}/>
            </View>
        </View>
    }

In this case, this.gotPhotoResponse is never triggered after allowing or denying the permissions. Any subsequent call to this.selectTakePhotoTapped or selectPickPhotoTapped works as expected.

I tried a workaround with checking the permissions individually with PermissionsAndroid from React-Native but it get messy as you can allow one permission and deny the other, launching react-native-image-picker afterwards still requires the 2 permissions in order to proceed.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:13

github_iconTop GitHub Comments

9reactions
pvanlieflandcommented, Jun 7, 2017

FYI, I got around the issue by “overriding” the react-native-image-picker module with a custom static class that keeps the same signature. I made a gist out of it: https://gist.github.com/pvanliefland/92046b17444a8760475e7cc0dae4fec7

If the library author is interested, I can work on a pull request.

6reactions
arelstonecommented, Apr 30, 2018

@Alexisvt I imported both react-native-image-picker and the gist provided by @pvanliefland and used Platform.OS to handle which one to use

import RNImagePicker from 'react-native-image-picker';
import ImagePickerAndroidWrapper from 'ImagePickerFromGist';

// Android doesn't get a callback when asking for permission.
// @see https://github.com/react-community/react-native-image-picker/issues/581
// `ImagePickerFromGist` is a wrapper for handling this.
const ImagePicker = Platform.OS === 'ios' ? RNImagePicker : ImagePickerAndroidWrapper;

export function launchImageLibrary(options) {
	return new Promise((resolve, reject) => {
		ImagePicker.launchImageLibrary(options, response => {
			if (response.didCancel) {
				return resolve();
			}
			if (response.error) {
				return reject(response.error);
			}
			resolve(response);
		});
	});
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Get permissions doesn't trigger callback · Issue #581 - GitHub
I tried a workaround with checking the permissions individually with PermissionsAndroid from React-Native but it get messy as you can allow one ...
Read more >
onRequestPermissionsResult callback is not triggering in an ...
I have tested with Log statements and in debug mode by setting breakpoints, but onRequestPermissionsResult is not at all getting called. Because ...
Read more >
Advanced Callbacks | Dash for Python Documentation | Plotly
Useful when multiple inputs can trigger the callback at the same time, or multiple properties of the same component can trigger the callback....
Read more >
ActivityCompat.OnRequestPermissionsResultCallback
Callback for the result from requesting permissions. This method is invoked for every call on requestPermissions .
Read more >
Navigator.getUserMedia() - Web APIs | MDN
If permission is denied, no compatible input devices exist, or any other error condition occurs, the error callback is executed with a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found