react-native-image-picker 2.x版本和3.x版本(最新版本)的使用詳解
一、react-native-image-picker 2.x版本的應用GitHub參考文檔
1. 安裝依賴(cmd進入項目根目錄執行命令)
yarn add react-native-image-picker
#RN >= 0.60(如果你的react-native版本 > = 0.60執行以下命令)
npx pod-install
#RN < 0.60(如果你的react-native版本< 0.60執行以下命令)
react-native link react-native-image-picker
2. Android配置(如需設置IOS自行查看GitHub文檔)
- 打開項目根目錄,然后進入該路徑android\app\src\main找到AndroidManifest.xml,添加以下兩行代碼:

1 <uses-permission android:name="android.permission.CAMERA" /> 2 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
以上設置好后,可以測試是否可以使用,若不行接着做以下設置......
- 進入項目根目錄,打開
android/settings.gradle文件,添加以下代碼:
1 include ':react-native-image-picker' 2 project(':react-native-image-picker').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-image-picker/android')
- 進入項目根目錄,打開android/app/build.gradle文件,添加以下代碼:
1 dependencies { 2 implementation project(':react-native-image-picker') 3 }
- 進入項目根目錄,打開android\app\src\main\java\com\你的項目名稱1\MainApplication.java文件,添加以下代碼:
1 import com.imagepicker.ImagePickerPackage; // <-- add this import 2 3 public class MainApplication extends Application implements ReactApplication { 4 @Override 5 protected List<ReactPackage> getPackages() { 6 return Arrays.<ReactPackage>asList( 7 new MainReactPackage(), 8 new ImagePickerPackage(), // <-- add this line 9 // OR if you want to customize dialog style 10 new ImagePickerPackage(R.style.my_dialog_style) 11 ); 12 } 13 }
- 進入項目根目錄,打開android\app\src\main\java\com\你的項目名稱1\MainActivity.java文件,添加以下代碼:
1 // 1. 添加以下兩行: 2 import com.imagepicker.permissions.OnImagePickerPermissionsCallback; // <- add this import 3 import com.facebook.react.modules.core.PermissionListener; // <- add this import 4 5 public class MainActivity extends ReactActivity { 6 // 2. 添加如下一行: 7 private PermissionListener listener; // <- add this attribute 8 9 /** 10 * Returns the name of the main component registered from JavaScript. 11 * This is used to schedule rendering of the component. 12 */ 13 @Override 14 protected String getMainComponentName() { 15 return "native_camera"; 16 } 17 }
老鐵以上設置好后即可正常使用 哦yes!!!
1.3.使用
import ImagePicker from 'react-native-image-picker';
const options = {
title: '請選擇',
cancelButtonTitle:'取消',
takePhotoButtonTitle:'拍照',
chooseFromLibraryButtonTitle:'從相冊選擇',
storageOptions: {
skipBackup: true,
path: 'images',
},
};
ImagePicker.showImagePicker(options, (response) => {
console.log('Response = ', response);
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
Alert.alert('自定義按鈕:' + response.customButton)
} else {
const source = { uri: response.uri };
Alert.alert(JSON.stringify(source))
console.log("source:" + JSON.stringify(source))
}
});
以上代碼是包含相機以及相冊(ui+功能),如果你想要直接觸發某項功能,可以調用以下方法:
1 function openCamera(){ 2 ImagePicker.launchCamera(options , (response) =>{ 3 console.log("response:" + response.path) 4 }) 5 } 6 7 function openImageLibrary(){ 8 ImagePicker.launchImageLibrary(options , (response) =>{ 9 console.log("response:" + response.path) 10 }) 11 }
二、react-native-image-picker 3.x版本的應用GitHub參考文檔
【注意:3.x版本不需要設置權限】
1.安裝依賴
1 //@latest選擇最新版本 2 yarn add react-native-image-picker@latest 3 4 # RN >= 0.60 5 cd ios && pod install 6 7 # RN < 0.60 8 react-native link react-native-image-picker
2.使用
1 import * as ImagePicker from 'react-native-image-picker';//--------> 切記注意導包格式 2 import React from 'react'; 3 import { 4 StyleSheet, 5 Text, 6 View, 7 Image, 8 ScrollView, 9 Platform, 10 SafeAreaView, 11 Button, 12 } from 'react-native'; 13 class App extends React.Component { 14 const [response, setResponse] = React.useState(null); 15 16 return ( 17 <SafeAreaView> 18 <ScrollView> 19 <Button 20 title="Take image" 21 onPress={() => 22 ImagePicker.launchCamera( 23 { 24 mediaType: 'photo', 25 includeBase64: false, 26 maxHeight: 200, 27 maxWidth: 200, 28 }, 29 (response) => { 30 setResponse(response); 31 }, 32 ) 33 } 34 /> 35 36 <Button 37 title="Select image" 38 onPress={() => 39 ImagePicker.launchImageLibrary( 40 { 41 mediaType: 'photo', 42 includeBase64: false, 43 maxHeight: 200, 44 maxWidth: 200, 45 }, 46 (response) => { 47 setResponse(response); 48 }, 49 ) 50 } 51 /> 52 53 <Button 54 title="Take video" 55 onPress={() => 56 ImagePicker.launchCamera({mediaType: 'video'}, (response) => { 57 setResponse(response); 58 }) 59 } 60 /> 61 62 <Button 63 title="Select video" 64 onPress={() => 65 ImagePicker.launchImageLibrary({mediaType: 'video'}, (response) => { 66 setResponse(response); 67 }) 68 } 69 /> 70 71 <View style={styles.response}> 72 <Text>Res: {JSON.stringify(response)}</Text> 73 </View> 74 75 {response && ( 76 <View style={styles.image}> 77 <Image 78 style={{width: 200, height: 200}} 79 source={{uri: response.uri}} 80 /> 81 </View> 82 )} 83 </ScrollView> 84 </SafeAreaView> 85 ); 86 } 87 88 const styles = StyleSheet.create({ 89 container: { 90 flex: 1, 91 alignItems: 'center', 92 backgroundColor: '#F5FCFF', 93 }, 94 button: { 95 marginVertical: 24, 96 marginHorizontal: 24, 97 }, 98 image: { 99 marginVertical: 24, 100 alignItems: 'center', 101 }, 102 response: { 103 marginVertical: 16, 104 marginHorizontal: 8, 105 }, 106 });
三、使用過程中可能會遇到的問題
1、報錯 提示 image-picker 和 sdk 版本問題 ,可以打開你項目下android/app/src/main/AndroidManifest.xml文件添加以下兩行代碼:

1 xmlns:tools="http://schemas.android.com/tools"
1 <uses-sdk tools:overrideLibrary="com.imagepicker"/>
如果出現bug 建議重新初始項目來測試。。。。。
2、關於response的返回值在github的api里也有詳細說明。
好了,介紹結束,其實還有個更高star的庫react-native-camera,而且是有人臉識別功能的,但我覺得普通項目用本文這個已經夠用了。
ps:如果跑項目報了一些莫名其妙的錯誤,可以試着刪除node_modules,然后在項目命令行里執行
yarn install
//或者輸入
npm instal
四、android 10.0 注意參考該博文【鏈接:https://www.cnblogs.com/freecolor/p/14006263.html 】添加相應的配置信息
