-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
117 lines (100 loc) · 3.46 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
import React, { useEffect, useState } from 'react';
import { Image, Pressable, StatusBar, Text, View } from 'react-native';
import { launchCamera, launchImageLibrary } from 'react-native-image-picker';
import DocumentPicker from 'react-native-document-picker';
const Gap = ({ height }) => (
<View style={{ height }} />
)
const App = () => {
const [url, setUrl] = useState("");
const [file, setFile] = useState(null);
const [type, setType] = useState("");
const preAction = () => {
setUrl("");
setType("");
setFile(null)
}
const onCamera = () => {
preAction();
launchCamera({
mediaType: 'photo',
}, (response) => {
if (!response.errorMessage && !response.didCancel) {
setUrl(response.uri)
setType("camera");
}
})
}
const onGallery = () => {
preAction();
launchImageLibrary({
mediaType: 'photo'
}, (response) => {
if (!response.errorMessage && !response.didCancel) {
setUrl(response.uri);
setType("gallery");
}
})
}
const onDocumentPicker = async () => {
try {
preAction();
const res = await DocumentPicker.pick({
type: [DocumentPicker.types.allFiles],
});
setType("document");
setFile({
uri: res.uri,
type: res.type,
name: res.name,
size: res.size
})
} catch (err) {
if (DocumentPicker.isCancel(err)) {
// User cancelled the picker, exit any dialogs or menus and move on
} else {
throw err;
}
}
}
return (
<View style={{ flex: 1, paddingTop: 50, backgroundColor: 'white' }} accessibilityLabel="app-root">
<StatusBar barStyle="dark-content" />
<View style={{ alignItems: 'center' }}>
<Pressable onPress={() => onCamera()} accessibilityLabel="btnCamera">
<Text>Image from Camera</Text>
</Pressable>
<Gap height={10} />
<Pressable onPress={() => onGallery()} accessibilityLabel="btnGallery">
<Text>Image from Gallery</Text>
</Pressable>
<Gap height={10} />
<Pressable onPress={() => onDocumentPicker()} accessibilityLabel="btnDocument">
<Text>File from Document Picker</Text>
</Pressable>
<Gap height={10} />
</View>
<View style={{ flex: 1, alignItems: 'center' }}>
<Text accessibilityLabel="btntype">{type}</Text>
{
url ? (
<Image
accessibilityLabel="imageview"
accessible={true}
source={{ uri: url }}
style={{ width: '100%', flex: 1 }}
/>
) : null
}
{
file ? (
<View style={{ width: 250 }} accessibilityLabel="fileView" accessible={true}>
<Text>{JSON.stringify(file)}</Text>
</View>
) : null
}
</View>
</View>
)
}
export default App;