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

Crash when opening camera #5

Open
oferRounds opened this issue Aug 26, 2018 · 6 comments
Open

Crash when opening camera #5

oferRounds opened this issue Aug 26, 2018 · 6 comments

Comments

@oferRounds
Copy link

http://crashes.to/s/3e0cab94c22

any idea?

@aseemnishad
Copy link
Owner

@oferRounds sorry, I didn't get much from the report, i can help you to debug, can you please share the code?

@oferRounds
Copy link
Author

oferRounds commented Aug 29, 2018

Hi @aseemnishad

Thanks fist of all!

I found out what’s the causing the issue: it’s the fact that FileProvider.getUriForFile is passing an authority which is specific for the scanlibrary, and on my case, it conflicts with other dependency, which also uses one.

I think the best would be to refactor the openCamera method like this, to pass the applicationContext.getPackageName() + ".provider" as the `authority:

public void openCamera() {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File file = createImageFile();
        boolean isDirectoryCreated = file.getParentFile().mkdirs();
        Log.d("", "openCamera: isDirectoryCreated: " + isDirectoryCreated);

        Uri tempFileUri;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            tempFileUri = Uri.fromFile(file);
        } else {
            Context applicationContext = getActivity().getApplicationContext();
            tempFileUri = FileProvider.getUriForFile(applicationContext,
                    applicationContext.getPackageName() + ".provider",
                    file);
        }

        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempFileUri);
        startActivityForResult(cameraIntent, ScanConstants.START_CAMERA_REQUEST_CODE);
    }

@aseemnishad
Copy link
Owner

@oferRounds Glad you found the solution and thank you for the suggestion, i would change it.

@oferRounds
Copy link
Author

awesome, thank you once again!

@SwagatRanjit84
Copy link

@aseemnishad @oferRounds Is it working? I am getting many erros!! Can you share your code?

@ElangoPrince
Copy link

@oferRounds I'm also having same problem. when opening camera , the app was crashed. ie: "Unfortumately app was stopped ".
any idea about this?

This is my code:
import React, {Component} from 'react';
import {
Platform,
StyleSheet,
Text,
View,
DeviceEventEmitter,
ToastAndroid,
TouchableOpacity,
Image
} from 'react-native';

import RNAndroidScanner from 'react-native-android-scanner';

export default class App extends Component {
constructor(props) {
super(props);
this.state = {
imageUri: null
};
}

componentWillMount = () => {
ToastAndroid.show('Listening for SCANNED_RESULT', ToastAndroid.SHORT);
DeviceEventEmitter.addListener(RNAndroidScanner.SCANNED_RESULT, this.onResult);
}
onScan(preference = 2){

RNAndroidScanner.startScan(preference);

}

onResult = (image) => {
ToastAndroid.show('onResult completion callback:', ToastAndroid.SHORT);
this.setState({imageUri: image.uri})
}

render() {
return (



React Native Android Scanner Demo

<TouchableOpacity onPress={() => this.onScan()}>

Start Scan




<Image
style={styles.image}
source={{
uri: this.state.imageUri
}}
resizeMode="contain"/>


);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF'
},
imageContainer: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center'
},
image: {
width: 150,
height: 150,
},
buttonContainer: {
flex: 1,
justifyContent: 'center',
marginTop: 50,
alignItems: 'center'
},
welcome: {
fontSize: 15,
textAlign: 'center',
margin: 10
},
scanButton: {
fontSize: 17,
textAlign: 'center',
color: '#333333',
marginBottom: 30
}
});

Error Image:
Screenshot_20190312-170959

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

4 participants