React Native--调用照相机 image-picker-react-native

参照网址https://github.com/marcshilling/react-native-image-picker

1.cmd到项目根目录,执行命令

npm install react-native-image-picker@latest --save


引组件

2.android/settings.gradle文件添加

include':react-native-image-picker'project(':react-native-image-picker').projectDir=newFile(settingsDir,'../node_modules/react-native-image-picker/android')

settingsDir参照配置文件settings.gradle中上面的修改

3.file: android/app/build.gradle

dependencies{...

compile project(':react-native-image-picker')

}

4.file: android/app/src/main/AndroidManifest.xml


授权拍照

5.// file: android/app/src/main/java/com/<...>/MainApplication.java

...importcom.imagepicker.ImagePickerPackage;// <-- add this importpublicclassMainApplicationextendsApplicationimplementsReactApplication{@OverrideprotectedListgetPackages() {returnArrays.asList(newMainReactPackage(),newImagePickerPackage()// <-- add this line);

}...}

6.Usage

varPlatform=require('react-native').Platform;varImagePicker=require('react-native-image-picker');// More info on all the options is below in the README...just some common use cases shown herevaroptions={

title:'Select Avatar',

customButtons:[

{name:'fb', title:'Choose Photo from Facebook'},

],

storageOptions:{

skipBackup:true,

path:'images'}

};/*** The first arg is the options object for customization (it can also be null or omitted for default options),* The second arg is the callback which sends object: response (more info below in README)*/ImagePicker.showImagePicker(options, (response)=>{console.log('Response = ', response);if(response.didCancel) {console.log('User cancelled image picker');

}elseif(response.error) {console.log('ImagePicker Error: ', response.error);

}elseif(response.customButton) {console.log('User tapped custom button: ', response.customButton);

}else{// You can display the image using either data...constsource={uri:'data:image/jpeg;base64,'+response.data, isStatic:true};// or a reference to the platform specific asset locationif(Platform.OS==='ios') {constsource={uri:response.uri.replace('file://',''), isStatic:true};

}else{constsource={uri:response.uri, isStatic:true};

}this.setState({

avatarSource:source

});

}

});

或者是参照上面链接中的example中的


使用
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,568评论 2 45
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,420评论 25 708
  • 人的一生都在不断的遇见和别离。我带着无数的憧憬和未知去迎接属于人生的每一个挑战。 来到下党,遇见你们,一切都那么美...
    是疯子先生阅读 547评论 0 2
  • 仍在回头 仍在踌躇 燕子剪尾有三岔路口 昏鸦鸣声里 河流烧得血红 风撩拨着几支嫩柳 那些人身披着银月的袍袖 牵着黑...
    重望阅读 160评论 0 0
  • title: hexo+github搭建最简单个人博客date: 2016-12-29 16:55:16tags:...
    _士心_阅读 561评论 0 0