gpt4 book ai didi

react-native-image-picker 在 android 10 中不起作用

转载 作者:行者123 更新时间:2023-12-04 01:13:35 25 4
gpt4 key购买 nike

我正在处理一个项目,我需要使用相机或从库中选择图像,因此使用了 native 图像选择器,它在开发模式下工作正常,在生产中它在 Android 10 中不起作用,但我尝试了很多来自 github 的解决方案喜欢权限
我在 list 中添加了 android:requestLegacyExternalStorage="true"但它导致了我和错误 android:requestLegacyExternalStorage not found 当我想在开发和生产中构建时
我将 sdk 目标更新为 29 同样的问题
我的图片选择器代码

const options = {
cancelButtonTitle: strings.cancel,
takePhotoButtonTitle: strings.takePicture,
chooseFromLibraryButtonTitle: strings.chooseFromLibrary,
title: strings.selectPhoto,
quality: 0.1,
noData: true,
storageOptions: {
skipBackup: true,
path: 'images',
cameraRoll: true,
waitUntilSaved: true,
},
};

const pickImage = () => {
const promise = new Promise(async (resolve, reject) => {
if (Platform.OS === 'android') {
try {
await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.CAMERA,
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
]);

const permissionCamera = await PermissionsAndroid.check(
'android.permission.CAMERA',
);
const permissionWriteStorage = await PermissionsAndroid.check(
'android.permission.WRITE_EXTERNAL_STORAGE',
);

if (!permissionCamera || !permissionWriteStorage) {
console.log('Failed to get the required permissions.');
}

const source = await openPicker();

resolve(source);
} catch (error) {
reject(error);
console.log('Failed to get the required permissions.');
}
} else {
try {
const source = await openPicker();
resolve(source);
} catch (error) {
reject(error);
console.log('Failed to get the required permissions.');
}
}
});
return promise;
};

const openPicker = () => {
const promise = new Promise((resolve, reject) => {
ImagePicker.showImagePicker(options, response => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
reject(response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
const source = {
uri: response.path ? `file://${response.path}` : response.uri,
name: response.fileName ? response.fileName : 'picture_0.jpg',
filename: response.fileName ? response.fileName : 'picture_0.jpg',
type: response.type,
};

resolve(source);
}
});
});

return promise;
};

const openCamera = () => {
const promise = new Promise((resolve, reject) => {
ImagePicker.launchCamera(options, response => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
reject(response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
const source = {
uri: response.path ? `file://${response.path}` : response.uri,
name: response.fileName ? response.fileName : 'picture_0.jpg',
filename: response.fileName ? response.fileName : 'picture_0.jpg',
type: response.type,
};

resolve(source);
}
});
});

return promise;
};

const openGallery = () => {
const promise = new Promise((resolve, reject) => {
ImagePicker.launchImageLibrary(options, response => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
reject(response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
const source = {
uri: response.path ? `file://${response.path}` : response.uri,
name: response.fileName ? response.fileName : 'picture_0.jpg',
filename: response.fileName ? response.fileName : 'picture_0.jpg',
type: response.type,
};

resolve(source);
}
});
});

return promise;
};

export {pickImage, openCamera, openGallery};

我的 list 权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

一些安卓信息
 buildToolsVersion = "28.0.3"
minSdkVersion = 16
compileSdkVersion = 28
targetSdkVersion = 29
那么有人有解决方案吗?

最佳答案

添加 android:requestLegacyExternalStorage="true"在 AndroidMenifest.xml 文件中适用于 Android 10。将其添加为 Application 标签的属性。
使用目标版本为 29。以下是 build.gradle 文件的一些信息。

  buildToolsVersion = "29.0.2"
minSdkVersion = 16
compileSdkVersion = 29
targetSdkVersion = 29
supportLibVersion = "29.0.0"
但是,此解决方案不适用于 Android 11。目前是最新版本。

关于react-native-image-picker 在 android 10 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64054870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com