调用摄像头拍照

react-native-image-picker的github官网

  1. yarn add react-native-image-picker
  2. 运行react-native link自动注册相关的组件到原生配置中
  3. 打开项目中的android->app->src->main->AndroidManifest.xml文件,在第8行添加如下配置:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  1. 打开项目中的android->app->src->main->java->com->当前项目名称文件夹->MainActivity.java文件,修改配置如下:
    package com.native_camera;
    import com.facebook.react.ReactActivity; // 1. 添加以下两行:
    import com.imagepicker.permissions.OnImagePickerPermissionsCallback; // <- add this import
    import com.facebook.react.modules.core.PermissionListener; // <- add this import public class MainActivity extends ReactActivity {
    // 2. 添加如下一行:
    private PermissionListener listener; // <- add this attribute /**
    * Returns the name of the main component registered from JavaScript.
    * This is used to schedule rendering of the component.
    */
    @Override
    protected String getMainComponentName() {
    return "native_camera";
    }
    }
  2. 在项目中添加如下代码:
    // 第1步:
import {View, Button, Image} from 'react-native'
import ImagePicker from 'react-native-image-picker'
var photoOptions = {
//底部弹出框选项
title: '请选择',
cancelButtonTitle: '取消',
takePhotoButtonTitle: '拍照',
chooseFromLibraryButtonTitle: '选择相册',
quality: 0.75,
allowsEditing: true,
noData: false,
storageOptions: {
skipBackup: true,
path: 'images'
}
} // 第2步:
constructor(props) {
super(props);
this.state = {
imgURL: ''
}
} // 第3步:
<Image source={{ uri: this.state.imgURL }} style={{ width: 200, height: 200 }}></Image>
<Button title="拍照" onPress={this.cameraAction}></Button> // 第4步:
cameraAction = () => {
ImagePicker.showImagePicker(photoOptions, (response) => {
console.log('response' + response);
if (response.didCancel) {
return
}
this.setState({
imgURL: response.uri
});
})
}
  1. 一定要退出之前调试的App,并重新运行react-native run-android进行打包部署;这次打包期间会下载一些jar的包,需要耐心等待!

me.js

import React, { Component } from 'react'
// 第1步:
import { View, Button, Image } from 'react-native'
// 导入拍照的包
import ImagePicker from 'react-native-image-picker'
// 创建拍照时候的配置对象
var photoOptions = {
//底部弹出框选项
title: '请选择',
cancelButtonTitle: '取消',
takePhotoButtonTitle: '拍照',
chooseFromLibraryButtonTitle: '选择相册',
quality: 0.75, // 照片的质量
allowsEditing: true, // 允许被编辑
noData: false, // 拍照时候不附带日期
storageOptions: { // 存储选项
skipBackup: true, // 在IOS平台中,会自动把 照片同步到 云端的存储,如果此项为 true,表示跳过 备份,不会把照片上传到 云端
path: 'images'
}
} export default class Me extends Component {
constructor(props) {
super(props);
this.state = {
imgURL: 'https://avatars0.githubusercontent.com/u/15337769?s=460&v=4' // 将来,拍摄的照片路径,会存到这里
}
} render() {
return <View style={{ alignItems: 'center', paddingTop: 70 }}>
<Image source={{ uri: this.state.imgURL }} style={{ width: 200, height: 200, borderRadius: 100 }}></Image>
<Button title="拍照" onPress={this.cameraAction}></Button>
</View>
} // 第4步:
cameraAction = () => {
ImagePicker.showImagePicker(photoOptions, (response) => {
console.log('response' + response);
if (response.didCancel) { // 点击了取消按钮,此时,用户没有拍照
return
} // 用户已经拍摄了一张照片了
this.setState({
imgURL: response.uri
});
})
}
}

react-native构建基本页面5---调用拍照摄像头的更多相关文章

  1. 【React Native】在原生和React Native间通信(RN调用原生)

    一.从React Native中调用原生方法(原生模块) 原生模块是JS中也可以使用的Objective-C类.一般来说这样的每一个模块的实例都是在每一次通过JS bridge通信时创建的.他们可以导 ...

  2. 从零学React Native之03页面导航

    之前我们介绍了RN相关的知识: 是时候了解React Native了 从零学React Native之01创建第一个程序 从零学React Native之02状态机 本篇主要介绍页面导航 上一篇文章给 ...

  3. react native (1) 新建页面并跳转

    新建页面 1.新建文件 import React from 'react'; import { Text } from 'react-native'; export default class tod ...

  4. react native onEndReached频繁多次调用问题

    今天被这个问题搞得头疼,写一个分页加载,但是listview的onEndReached方法老是被频繁调用,知道加载完所有的分页数据才停止. <ListView automaticallyAdju ...

  5. react native tap切换页面卡顿

    问题描述:做一个页面,左边是导航,每次点击一个菜单,右边立即显示出对应的视图,数据会重新过滤,使用setState 更新视图,会卡顿 解决办法: InteractionManager.runAfter ...

  6. React Native 中 跨页面间通信解决方案之 react-native-event-bus

    https://github.com/crazycodeboy/react-native-event-bus 用法: A页面和B页面中都有相同的列表,点击B页面中的收藏按钮,A页面会跟着更新 impo ...

  7. React Native登录注册页面实现空白处收起键盘

    其实很简单,直接使用ScrollView作为父视图即可.有木有很神奇啊,以前都还不知道呢.....

  8. 【React Native】某个页面禁用物理返回键

    1.引入组件 import { BackHandler, } from 'react-native'; 2.添加监听 componentDidMount(): void { BackHandler.a ...

  9. React Native 简介:用 JavaScript 搭建 iOS 应用 (1)

    [编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...

随机推荐

  1. 维基逃离MySQL 力挺开源数据库 MariaDB

    近日全球著名百科类网站维基百科宣布,将不会再用MySQL数据库,据国外媒体报道,很多年,MySQL一直是热门的开源数据库,不过在被甲骨文收购后,面临闭源的风险.因此维基百科将切换到另外一款开源数据库M ...

  2. tensorflow打印可用设备列表

    from tensorflow.python.client import device_libprint(device_lib.list_local_devices())

  3. Go语言基础之接口(面向对象编程下)

    1 接口 1.1 接口介绍 接口(interface)是Go语言中核心部分,Go语言提供面向接口编程,那么接口是什么? 现实生活中,有许多接口的例子,比如说电子设备上的充电接口,这个充电接口能干什么, ...

  4. HTML连载68-形变中心点、形变中心轴

    一. 形变中心点介绍 <style> ul li { width: 100px; height: 100px; list-style: none; float:left; margin:0 ...

  5. Android中点击按钮启动另一个Activity以及Activity之间传值

    场景 点击第一个Activity中的按钮,启动第二个Activity,关闭第二个Activity,返回到第一个Activity. 在第一个Activity中给第二个Activity传递值,第二个Act ...

  6. ORACLE中如何找出大表分布在哪些数据文件中?

    ORACLE中如何找出大表分布在哪些数据文件中?   在ORACLE数据中,我们能否找出一个大表的段对象分布在哪些数据文件中呢? 答案是可以,我们可以用下面脚本来找出对应表的区.段分别位于哪些数据文件 ...

  7. 使用PropTypes进行类型检查

    原文地址 1.组件特殊属性——propTypes 对Component设置propTypes属性,可以为Component的props属性进行类型检查. import PropTypes from ' ...

  8. O2O外卖玩众包 开放平台难解标准之痛

    开放平台难解标准之痛" title="O2O外卖玩众包 开放平台难解标准之痛">  有一种怪现象一直是国内互联网企业摆脱不了的附骨之疽--不管规模大小,总是削尖了脑 ...

  9. PMP-番外篇-PMP工具与技术目录

    ########################################################### 这里先总结所有工具和技术,让大家有一个整体的概念. 也可以当作一个工具和技术查询 ...

  10. 使用CSV Data Set Config配置原件,参数化数据

    对接口数据的参数化方式大概有三种方式,1:jmeter内置函数:2:借助CSV Data Set Config配置原件:3:jdbc连接数据库,使用数据表字段 此处主要讲第二种:借助CSV Data ...