React Native CameraRoll being undefined



这是我使用react原生社区相机卷的功能组件。它主要是从github的自述文件中复制的示例:https://github.com/react-native-cameraroll/react-native-cameraroll

我确实在安卓11手机上使用expo应用程序来测试和调试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>
);
}

当我第一次按下按钮时,我得到了权限提示,我点击了允许。然后我得到了这个错误:

[未处理的承诺拒绝:类型错误:无法读取未定义的属性(读取"getPhotos")]在saveToCameraRoll中的node_modules/@react native community/cameraroll/js/cameraroll.js:205:32在组件/CameraRollButton.tsx:34:19 in onPress

我通过sudo npm i @react-native-community/cameraroll --save安装了模块。我稍后也尝试执行自述文件中的这一行:react-native link @react-native-community/cameraroll && npx pod-install。它似乎没有任何作用,据我所知,我不需要它,因为它有自动链接。

我还删除了node_modules并重新安装了所有内容。还是同样的错误。我还尝试通过expo install安装模块。

我仍然不确定问题出在哪里,但我想使用expo可能是个问题。我忘了expo有很多模块,所以我最终使用了https://docs.expo.dev/versions/v42.0.0/sdk/media-library/没有任何问题。

最新更新