react-native-image-picker launchCamera 在 android 中不工作
react-native-image-picker launchCamera in not working in android
我在 React Native 中使用 "react-native-image-picker": "^3.0.1"
来捕获图像。但是我在 android 9.
中打开相机时出错
我收到错误:
{"errorCode": "others", "errorMessage": "This library does not require Manifest.permission.CAMERA, if you add this permission in manifest then you have to obtain the same."}
这是我的代码
ImagePicker.launchCamera(
{
includeBase64: false,
mediaType: 'photo',
quality: 0.8,
},
async (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
}
},
);
在拍摄图像之前,请向用户请求相机许可。在Android以上的marshmallow版本中,你应该请求运行时间权限,这被称为危险权限。
const requestCameraPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: "App Camera Permission",
message:"App needs access to your camera "
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Camera permission given");
} else {
console.log("Camera permission denied");
}
} catch (err) {
console.warn(err);
}
};
然后,如果获得许可,则在 if
内调用
ImagePicker.launchCamera
这对我有帮助
向 android\app\src\main\AndroidManifest.xml 添加选项 -> 应用程序 部分
-> 参数 android:requestLegacyExternalStorage="true"
android\app\src\main\AndroidManifest.xml
...
<application
...
android:requestLegacyExternalStorage="true"
...>
We need to add run time permissions for the react-native-image-picker . We also need to add seperate permission request for camera and external storage as.please check out below code worked for me .
const grantedcamera = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: "App Camera Permission",
message:"App needs access to your camera ",
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
const grantedstorage = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: "App Camera Permission",
message:"App needs access to your camera ",
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
if (grantedcamera === PermissionsAndroid.RESULTS.GRANTED && grantedstorage === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Camera & storage permission given");
var options = {
mediaType: 'photo', //to allow only photo to select ...no video
saveToPhotos:true, //to store captured photo via camera to photos or else it will be stored in temp folders and will get deleted on temp clear
includeBase64:false,
};
launchCamera (options, (res) => {
console.log('Response = ', res);
if (res.didCancel) {
console.log('User cancelled image picker');
} else if (res.error) {
console.log('ImagePicker Error: ', res.error);
} else if (res.customButton) {
console.log('User tapped custom button: ', res.customButton);
alert(res.customButton);
} else {
// let source = res;
// var resourcePath1 = source.assets[0].uri;
const source = { uri: res.uri };
console.log('response', JSON.stringify(res));
setImageSource(source.uri);
}
});
} else {
console.log("Camera permission denied");
}
我在 React Native 中使用 "react-native-image-picker": "^3.0.1"
来捕获图像。但是我在 android 9.
我收到错误:
{"errorCode": "others", "errorMessage": "This library does not require Manifest.permission.CAMERA, if you add this permission in manifest then you have to obtain the same."}
这是我的代码
ImagePicker.launchCamera(
{
includeBase64: false,
mediaType: 'photo',
quality: 0.8,
},
async (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
}
},
);
在拍摄图像之前,请向用户请求相机许可。在Android以上的marshmallow版本中,你应该请求运行时间权限,这被称为危险权限。
const requestCameraPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: "App Camera Permission",
message:"App needs access to your camera "
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Camera permission given");
} else {
console.log("Camera permission denied");
}
} catch (err) {
console.warn(err);
}
};
然后,如果获得许可,则在 if
内调用
ImagePicker.launchCamera
这对我有帮助
向 android\app\src\main\AndroidManifest.xml 添加选项 -> 应用程序 部分 -> 参数 android:requestLegacyExternalStorage="true"
android\app\src\main\AndroidManifest.xml
...
<application
...
android:requestLegacyExternalStorage="true"
...>
We need to add run time permissions for the react-native-image-picker . We also need to add seperate permission request for camera and external storage as.please check out below code worked for me .
const grantedcamera = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: "App Camera Permission",
message:"App needs access to your camera ",
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
const grantedstorage = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: "App Camera Permission",
message:"App needs access to your camera ",
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
if (grantedcamera === PermissionsAndroid.RESULTS.GRANTED && grantedstorage === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Camera & storage permission given");
var options = {
mediaType: 'photo', //to allow only photo to select ...no video
saveToPhotos:true, //to store captured photo via camera to photos or else it will be stored in temp folders and will get deleted on temp clear
includeBase64:false,
};
launchCamera (options, (res) => {
console.log('Response = ', res);
if (res.didCancel) {
console.log('User cancelled image picker');
} else if (res.error) {
console.log('ImagePicker Error: ', res.error);
} else if (res.customButton) {
console.log('User tapped custom button: ', res.customButton);
alert(res.customButton);
} else {
// let source = res;
// var resourcePath1 = source.assets[0].uri;
const source = { uri: res.uri };
console.log('response', JSON.stringify(res));
setImageSource(source.uri);
}
});
} else {
console.log("Camera permission denied");
}