OpenCamera.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import React from 'react'
  2. import {
  3. TouchableOpacity,
  4. StyleSheet,
  5. Platform,
  6. View,
  7. } from 'react-native'
  8. import SyanImagePicker from 'react-native-syan-image-picker-zzly'
  9. import RNFS from 'react-native-fs';
  10. class OpenCamera extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. loading: false,
  15. visible: false,
  16. }
  17. }
  18. render() {
  19. return (
  20. <View>
  21. <TouchableOpacity
  22. onPress={() => this.showImagePicker()}
  23. style={[this.props.style, styles.cameraBtn]}>
  24. <View>
  25. {this.props.children}
  26. </View>
  27. </TouchableOpacity>
  28. </View>
  29. )
  30. }
  31. showImagePicker = () => {
  32. const options = {
  33. isOpenCheckNumStyle: true,
  34. isWeChatStyle: true,
  35. imageCount: this.props.maxFiles, // 最大选择图片数目,默认6
  36. isCrop: false, // 是否允许裁剪,imageCount 为1才生效
  37. };
  38. SyanImagePicker.openCamera(options, (err, selectedPhotos) => {
  39. if (err) {
  40. // 取消选择
  41. return;
  42. }
  43. // 选择成功,渲染图片
  44. this.setState({visible: false,})
  45. let photos = {}
  46. for (const {uri} of selectedPhotos) {
  47. let file;
  48. if (Platform.OS === 'android') {
  49. file = uri
  50. } else {
  51. file = uri.replace('file://', '')
  52. }
  53. const file_list = file.split('/');
  54. let fileArr = file.split('.');
  55. let fileName = file_list[file_list.length - 1]
  56. let mime = `.${fileArr[fileArr.length - 1]}`
  57. if (mime.indexOf('mmexport') > -1) {
  58. mime = '.jpg'
  59. fileName = fileName.split('.')[0] + mime
  60. }
  61. photos =
  62. {
  63. img_id: '',
  64. type: 'img',
  65. mime: mime,
  66. uri: file,
  67. fileName: fileName
  68. }
  69. }
  70. const itemID = this.props.itemID;
  71. this.props.onFileUpload(photos, itemID)
  72. // var path = RNFS.DocumentDirectoryPath + '/test.txt';
  73. // console.log(22222222, this.props.photos)
  74. // console.log(22222222, selectedPhotos[0].uri)
  75. // RNFS.unlink(selectedPhotos[0].uri)
  76. // .then(() => {
  77. // console.log('444444444FILE DELETED444444444');
  78. // })
  79. // // `unlink` will throw an error, if the item to unlink does not exist
  80. // .catch((err) => {
  81. // console.log(555555555, err.message);
  82. // });
  83. // const dirPicutures = '/storage/emulated/0/Android/data/com.zzliaoyuan.decorate_touch/files/Pictures/'+this.props.photos[0].fileName;
  84. // console.log(3333333333, dirPicutures)
  85. // RNFS.exists(dirPicutures)
  86. // .then((result) => {
  87. // console.log("file exists: ", result);
  88. //
  89. // if (result) {
  90. // return RNFS.unlink(dirPicutures)
  91. // .then(() => {
  92. // console.log('FILE DELETED');
  93. // })
  94. // // `unlink` will throw an error,if the item to unlink does not exist
  95. // .catch((err) => {
  96. // console.log(err.message);
  97. // });
  98. // }
  99. //
  100. // })
  101. // .catch((err) => {
  102. // console.log(err.message);
  103. // });
  104. })
  105. }
  106. // showImagePicker1 = () => {
  107. // ImagePicker.openPicker({
  108. // mediaType: 'photo',
  109. // multiple: true,
  110. // }).then(images => {
  111. // this.setState({visible: false,})
  112. // for (const {path} of images) {
  113. // let file;
  114. // if (Platform.OS === 'android') {
  115. // file = path
  116. // } else {
  117. // file = path.replace('file://', '')
  118. // }
  119. // const file_list = file.split('/');
  120. // let fileArr = file.split('.');
  121. // let fileName = file_list[file_list.length - 1]
  122. // let mime = `.${fileArr[fileArr.length - 1]}`
  123. // if (mime.indexOf('mmexport') > -1) {
  124. // mime = '.jpg'
  125. // fileName = fileName.split('.')[0] + mime
  126. // }
  127. // this.props.photos.push(
  128. // {
  129. // img_id: '',
  130. // type: 'img',
  131. // mime: mime,
  132. // uri: file,
  133. // fileName: fileName
  134. // }
  135. // )
  136. // }
  137. // if (this.props.maxFiles) {
  138. // if (this.props.photos.length > this.props.maxFiles) {
  139. // Toast.info('仅能上传' + this.props.maxFiles + '张照片')
  140. // const photos = this.props.photos.slice(0, this.props.maxFiles)
  141. // this.props.onFileUpload(photos)
  142. // } else {
  143. // this.props.onFileUpload(this.props.photos)
  144. // }
  145. // }
  146. //
  147. // }).catch((e) => {
  148. // this.setState({visible: false,})
  149. // // console.log(11111111113,e)
  150. // // Toast.info('未选择文件或文件异常!',2)
  151. // });
  152. // }
  153. }
  154. const styles = StyleSheet.create({
  155. cameraBtn: {
  156. padding: 5
  157. },
  158. count: {
  159. color: '#fff',
  160. fontSize: 12
  161. },
  162. fullBtn: {
  163. justifyContent: 'center',
  164. alignItems: 'center',
  165. backgroundColor: '#fff'
  166. },
  167. countBox: {
  168. position: 'absolute',
  169. right: -5,
  170. top: -5,
  171. alignItems: 'center',
  172. backgroundColor: '#34A853',
  173. width: 16,
  174. height: 16,
  175. borderRadius: 8,
  176. justifyContent: 'center'
  177. }
  178. });
  179. export default OpenCamera;