-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
100 lines (81 loc) · 2.1 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React from 'react';
import {
ActivityIndicator,
Button,
Clipboard,
Image,
Share,
StatusBar,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
import uuid from 'uuid';
import { Constants, ImagePicker, Permissions } from 'expo';
import ApiKeys from './constants/ApiKeys';
import * as firebase from 'firebase'; //npm install --save firebase
// ref https://github.com/expo/firebase-storage-upload-example/blob/master/App.js
export default class App extends React.Component {
constructor(props){
super(props);
state = {
image: null,
uploading: false,
};
if(!firebase.apps.length) { firebase.initializeApp(ApiKeys.FirebaseConfig);}
}
async componentDidMount() {
await Permissions.askAsync(Permissions.CAMERA_ROLL);
await Permissions.askAsync(Permissions.CAMERA);
}
_pickImage = async () => {
let pickerResult = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
});
this._handleImagePicked(pickerResult);
};
_handleImagePicked = async pickerResult => {
try {
this.setState({ uploading: true });
if (!pickerResult.cancelled) {
uploadUrl = await uploadImageAsync(pickerResult.uri);
this.setState({ image: uploadUrl });
}
} catch (e) {
console.log(e);
alert('Upload failed, sorry :(');
} finally {
this.setState({ uploading: false });
}
};
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
onPress={this._pickImage}
title="Pick an image from camera roll"
/>
</View>
);
}
}
async function uploadImageAsync(uri) {
const response = await fetch(uri);
const blob = await response.blob();
const ref = firebase
.storage()
.ref()
.child(uuid.v4());
const snapshot = await ref.put(blob);
return snapshot.downloadURL;
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});