Skip to content

Instantly share code, notes, and snippets.

@muhozi
Last active January 9, 2019 19:59
Show Gist options
  • Save muhozi/4512bbbabdd75a690f0b23b00d0f8a0a to your computer and use it in GitHub Desktop.
Save muhozi/4512bbbabdd75a690f0b23b00d0f8a0a to your computer and use it in GitHub Desktop.
App.js - Test if image picker works
import React, { Component } from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Image } from 'react-native';
import ImagePicker from 'react-native-image-picker';
const options = {
title: 'Select Image',
storageOptions: {
skipBackup: true,
path: 'images'
}
};
export default class App extends Component {
state = {
imgSource: ''
};
/**
* Select image method
*/
pickImage = () => {
ImagePicker.showImagePicker(options, response => {
if (response.didCancel) {
alert('You cancelled image picker ๐Ÿ˜Ÿ');
} else if (response.error) {
alert('And error occured: ', response.error);
} else {
const source = { uri: response.uri };
this.setState({
imgSource: source
});
}
});
};
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>React Native Firebase Image Upload </Text>
<Text style={styles.instructions}>Hello ๐Ÿ‘‹, Let us upload an Image</Text>
{/** Select Image button */}
<TouchableOpacity style={styles.btn} onPress={this.pickImage}>
<View>
<Text style={styles.btnTxt}>Pick image</Text>
</View>
</TouchableOpacity>
{/** Display selected image */}
{this.state.imgSource ? (
<Image
source={this.state.imgSource}
style={styles.image}
/>
) : (
<Text>Select an Image!</Text>
)}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5
},
btn: {
borderWidth: 1,
paddingLeft: 20,
paddingRight: 20,
paddingTop: 10,
paddingBottom: 10,
borderRadius: 20,
borderColor: 'rgba(0,0,0,0.3)',
backgroundColor: 'rgb(68, 99, 147)'
},
btnTxt: {
color: '#fff'
},
image: {
marginTop: 20,
minWidth: 200,
height: 200
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment