Skip to content

Instantly share code, notes, and snippets.

@vaibhavgehani
Created August 5, 2022 07:16
Show Gist options
  • Select an option

  • Save vaibhavgehani/65dccff5e88a526a18df569b86190e77 to your computer and use it in GitHub Desktop.

Select an option

Save vaibhavgehani/65dccff5e88a526a18df569b86190e77 to your computer and use it in GitHub Desktop.
import React, { Fragment, Component, useState } from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Image,
Button,
Dimensions,
TouchableOpacity
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';
export default App = () => {
const [fileData, setFileData] = useState(null);
const [fileUri, setFileUri] = useState(null);
const renderFileData = () => {
if (fileData) {
return <Image source={{ uri: 'data:image/jpeg;base64,' + fileData }}
style={styles.images}
/>
} else {
return <Image source={require('./assets/dummy.png')}
style={styles.images}
/>
}
}
const renderFileUri = () => {
if (fileUri) {
return <Image
source={{ uri: fileUri }}
style={styles.images}
/>
} else {
return <Image
source={require('./assets/galeryImages.jpeg')}
style={styles.images}
/>
}
}
const launchNativeCamera = () => {
let options = {
includeBase64: true,
storageOptions: {
skipBackup: true,
path: 'images',
},
};
launchCamera(options, (response) => {
console.log('Response = ', response);
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.errorCode) {
console.log('ImagePicker Error: ', response.errorMessage);
} else {
const source = { uri: response.uri };
console.log('response', JSON.stringify(response));
setFileData(response.assets[0].base64);
setFileUri(response.assets[0].uri)
}
});
}
const launchNativeImageLibrary = () => {
let options = {
includeBase64: true,
storageOptions: {
skipBackup: true,
path: 'images',
},
};
launchImageLibrary(options, (response) => {
console.log('Response = ', response);
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.errorCode) {
console.log('ImagePicker Error: ', response.error);
} else {
const source = { uri: response.assets.uri };
console.log('response', JSON.stringify(response));
setFileData(response.assets[0].base64);
setFileUri(response.assets[0].uri)
}
});
}
return (
<Fragment>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<View style={styles.body}>
<Text style={{textAlign:'center',fontSize:20,paddingBottom:10}} >Pick Images from Camera & Gallery</Text>
<View style={styles.ImageSections}>
<View>
{renderFileData()}
<Text style={{textAlign:'center'}}>Base 64 String</Text>
</View>
<View>
{renderFileUri()}
<Text style={{textAlign:'center'}}>File Uri</Text>
</View>
</View>
<View style={styles.btnParentSection}>
<TouchableOpacity onPress={() => {launchNativeCamera()}} style={styles.btnSection} >
<Text style={styles.btnText}>Directly Launch Camera</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => {launchNativeImageLibrary()}} style={styles.btnSection} >
<Text style={styles.btnText}>Directly Launch Image Library</Text>
</TouchableOpacity>
</View>
</View>
</SafeAreaView>
</Fragment>
)
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
body: {
backgroundColor: Colors.white,
justifyContent: 'center',
borderColor: 'black',
borderWidth: 1,
height: Dimensions.get('screen').height - 20,
width: Dimensions.get('screen').width
},
ImageSections: {
display: 'flex',
flexDirection: 'row',
paddingHorizontal: 8,
paddingVertical: 8,
justifyContent: 'center'
},
images: {
width: 150,
height: 150,
borderColor: 'black',
borderWidth: 1,
marginHorizontal: 3
},
btnParentSection: {
alignItems: 'center',
marginTop:10
},
btnSection: {
width: 225,
height: 50,
backgroundColor: '#DCDCDC',
alignItems: 'center',
justifyContent: 'center',
borderRadius: 3,
marginBottom:10
},
btnText: {
textAlign: 'center',
color: 'gray',
fontSize: 14,
fontWeight:'bold'
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment