react-native-image-picker配置

测试环境:android   react-native 0.47.1 

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

2、react-native link

Android:
1、在文件   android/settings.gradle中 添加
include ':react-native-image-picker'
project(':react-native-image-picker').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-image-picker/android')

2、更改 android/build.gradle:
buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.+' // <- USE 2.2.+ version
    }
    ...
}
...

3、更改   android/gradle/wrapper/gradle-wrapper.properties :
...
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip


4、在文件 android/app/build.gradle中添加

dependencies {
    compile project(':react-native-image-picker')
}

5、在文件 android/app/src/main/AndroidManifest.xml中添加

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

6、在文件 MainApplication.java中添加

import com.imagepicker.ImagePickerPackage; // <-- add this import

public class MainApplication extends Application implements ReactApplication {
    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new ImagePickerPackage() // <-- add this line
            // OR if you want to customize dialog style
            new ImagePickerPackage(R.style.my_dialog_style)
        );
    }
}


7、配置弹窗样式 android/app/res/values/themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="DefaultExplainingPermissionsTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <!-- Used for the buttons -->
        <item name="colorAccent">#61c3e7</item>
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">#3d3d3d</item>
        <!-- Used for the background -->
        <item name="android:background">@android:color/white</item>
    </style>
</resources>


使用:

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'
    }
}

 <Text onPress={()=>this.openMycamera()}>选择文件</Text>


openMycamera = () =>{
        ImagePicker.showImagePicker(photoOptions,(response) =>{
            console.log('response'+response);
            if (response.didCancel){
                return
            }

        })
    }



结果:






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native 中的 ImagePicker 组件获取到的图片是以 URI 的形式返回的,而不是公共路径。如果您需要将图片保存到公共路径中,可以使用 `react-native-fs` 这个第三方库,用于读取和写入文件系统。 以下是一个简单的示例,演示如何将 ImagePicker 返回的图片保存到公共路径中: ```javascript import React, { useState } from 'react'; import { Button, Image, View } from 'react-native'; import ImagePicker from 'react-native-image-picker'; import RNFS from 'react-native-fs'; const App = () => { const [image, setImage] = useState(null); const handleChoosePhoto = () => { const options = { mediaType: 'photo', quality: 1, }; ImagePicker.launchImageLibrary(options, response => { if (response.uri) { const folderPath = RNFS.DownloadDirectoryPath; const fileName = 'myImage.jpg'; const filePath = `${folderPath}/${fileName}`; RNFS.copyFile(response.uri, filePath).then(() => { setImage(filePath); }); } }); }; return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> {image && ( <Image source={{ uri: `file://${image}` }} style={{ width: 200, height: 200 }} /> )} <Button title="Choose Photo" onPress={handleChoosePhoto} /> </View> ); }; export default App; ``` 在上面的示例中,我们使用 `react-native-fs` 中的 `copyFile` 方法将 ImagePicker 返回的图片复制到公共路径中,并将公共路径作为 state 更新。最后,我们使用 `file://` 协议将图片加载到 Image 组件中。 希望这能帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值