/
App.js
189 lines (166 loc) · 4.65 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import * as React from "react";
import { ActivityIndicator, Button, Image, StatusBar, Text, View } from "react-native";
import * as ImagePicker from "expo-image-picker";
export default class App extends React.Component {
state = {
image: null,
uploading: false,
};
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text
style={{
fontSize: 20,
marginBottom: 20,
textAlign: "center",
marginHorizontal: 15,
}}
>
Example: Upload ImagePicker result
</Text>
{this._maybeRenderControls()}
{this._maybeRenderUploadingIndicator()}
{this._maybeRenderImage()}
<StatusBar barStyle="default" />
</View>
);
}
_maybeRenderUploadingIndicator = () => {
if (this.state.uploading) {
return <ActivityIndicator animating size="large" color="#0000ee" />;
}
};
_maybeRenderControls = () => {
if (!this.state.uploading) {
return (
<View>
<View style={{ marginVertical: 8 }}>
<Button
onPress={this._pickImage}
title="Pick an image from camera roll"
/>
</View>
<View style={{ marginVertical: 8 }}>
<Button onPress={this._takePhoto} title="Take a photo" />
</View>
</View>
);
}
};
_maybeRenderImage = () => {
if (this.state.image) {
return (
<View
style={{
marginTop: 30,
width: 250,
borderRadius: 3,
elevation: 2,
shadowColor: "rgba(0,0,0,1)",
shadowOpacity: 0.2,
shadowOffset: { width: 4, height: 4 },
shadowRadius: 5,
}}
>
<View
style={{
borderTopRightRadius: 3,
borderTopLeftRadius: 3,
overflow: "hidden",
}}
>
<Image
source={{ uri: this.state.image }}
style={{ width: 250, height: 250 }}
/>
</View>
<Image
style={{ paddingVertical: 10, paddingHorizontal: 10 }}
source={{ uri: this.state.image }}
/>
</View>
);
}
};
_askPermission = async (failureMessage) => {
const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status === "denied") {
alert(failureMessage);
}
};
_askCameraPermission = async (failureMessage) => {
const { status } = await ImagePicker.requestCameraPermissionsAsync();
if (status === "denied") {
alert(failureMessage);
}
};
_takePhoto = async () => {
await this._askCameraPermission(
"We need the camera permission to take a picture..."
);
let pickerResult = await ImagePicker.launchCameraAsync({
allowsEditing: true,
aspect: [4, 3],
});
this._handleImagePicked(pickerResult);
};
_pickImage = async () => {
await this._askPermission(
"We need the camera-roll permission to read pictures from your phone..."
);
let pickerResult = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
});
this._handleImagePicked(pickerResult);
};
_handleImagePicked = async (pickerResult) => {
let uploadResponse, uploadResult;
try {
this.setState({ uploading: true });
if (!pickerResult.cancelled) {
uploadResponse = await uploadImageAsync(pickerResult.uri);
uploadResult = await uploadResponse.json();
console.log({ uploadResult });
this.setState({ image: uploadResult.files.photo });
}
} catch (e) {
console.log({ uploadResponse });
console.log({ uploadResult });
console.log({ e });
alert("Upload failed, sorry :(");
} finally {
this.setState({ uploading: false });
}
};
}
async function uploadImageAsync(uri) {
let apiUrl = "https://httpbin.org/post";
// Note:
// Uncomment this if you want to experiment with local server
//
// if (Constants.isDevice) {
// apiUrl = `https://your-ngrok-subdomain.ngrok.io/upload`;
// } else {
// apiUrl = `http://localhost:3000/upload`
// }
let uriArray = uri.split(".");
let fileType = uriArray[uriArray.length - 1];
let formData = new FormData();
formData.append("photo", {
uri,
name: `photo.${fileType}`,
type: `image/${fileType}`,
});
let options = {
method: "POST",
body: formData,
mode: 'cors',
headers: {
Accept: "application/json",
"Content-Type": "multipart/form-data",
},
};
return fetch(apiUrl, options);
}