【发布时间】:2021-11-30 00:19:20
【问题描述】:
这是我使用 react native 社区相机胶卷的功能组件。它主要是从 github 自述文件中的示例复制而来的:https://github.com/react-native-cameraroll/react-native-cameraroll
我确实在我的 android 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>
);
}
当我第一次按下按钮时,我得到了我单击允许的权限提示。然后我得到了这个错误:
[未处理的承诺拒绝:TypeError:无法读取未定义的属性(读取'getPhotos')] 在 node_modules/@react-native-community/cameraroll/js/CameraRoll.js:205:32 在 saveToCameraRoll 在组件/CameraRollButton.tsx:34:19 在 onPress
我通过sudo npm i @react-native-community/cameraroll --save 安装了模块。我后来也尝试从自述文件中执行这一行:react-native link @react-native-community/cameraroll && npx pod-install。它似乎没有做任何事情,据我所知,我不需要它,因为有自动链接。
我还删除了 node_modules 并重新安装了所有内容。还是同样的错误。我还尝试通过expo install 安装模块。
【问题讨论】:
标签: typescript react-native expo camera-roll