这是我的功能组件,使用的反应本机社区相机卷。它主要是从github:https://github.com/react-native-cameraroll/react-native-cameraroll自述的自述文件中复制出来的。
我确实使用我的android 11手机上的世博应用程序来测试和调试rn应用程序。
import * as React from 'react';
import {PermissionsAndroid, Platform, ScrollView, Image} from 'react-native';
import {Text, View} from "./Themed";
import CameraRoll, {PhotoIdentifier} from '@react-native-community/cameraroll';
import {useState} from 'react';
type CameraRollButtonProps = {}
export default function CameraRollButton({}: CameraRollButtonProps) {
const [photos, setPhotos] = useState<PhotoIdentifier[]>([]);
async function hasAndroidPermission() {
const permission = PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE;
const hasPermission = await PermissionsAndroid.check(permission);
if (hasPermission) {
return true;
}
const status = await PermissionsAndroid.request(permission);
return status === 'granted';
}
const onPress = async () => {
if (Platform.OS === "android" && !(await hasAndroidPermission())) {
return;
}
CameraRoll.getPhotos({
first: 20,
assetType: 'Photos',
})
.then(r => {
setPhotos(r.edges);
})
.catch((err) => {
console.log("error catched: " + err);
})
};
return (
<View>
<Text onPress={onPress}>
Camera Roll
</Text>
<ScrollView>
{photos.map((p, i) =>
<Image
key={i}
style={{
width: 300,
height: 100,
}}
source={{uri: p.node.image.uri}}
/>
)}
</ScrollView>
</View>
);
}当我首先按下按钮时,我得到了权限提示,我点击了“允许”。然后我得到了一个错误:
未处理的承诺拒绝: node_modules/@react-native-community/cameraroll/js/CameraRoll.js:205:32 : TypeError:无法在组件/CameraRollButton.tsx:34:19在onPress中读取未定义属性(读取“getPhotos”)
我通过sudo npm i @react-native-community/cameraroll --save安装了模块。我还试图在以后的自述文件中执行这一行:react-native link @react-native-community/cameraroll && npx pod-install。它似乎什么都没做,据我所知,我不需要它,因为有自动链接。
我还删除了node_modules并重新安装了所有东西。还是同样的错误。我还尝试通过expo install安装模块。
发布于 2021-10-15 07:59:53
我仍然不知道是什么问题,但我想使用世博会可能是一个问题。我忘了世博有很多模块,所以我最终使用了https://docs.expo.dev/versions/v42.0.0/sdk/media-library/,没有任何问题。
https://stackoverflow.com/questions/69522971
复制相似问题