CameraRoll.getAlbums({
assetType:"All"
}).then( list => {
console.log(list.length)
}).catch((err) => {
console.log(err)
})
我从CameraRoll尝试过此操作,但它返回了一个空列表。
答案 0 :(得分:0)
首先请确保您添加了正确的权限,然后再进行构建。 还要检查您的设备是否有任何文件。
然后您可以遵循以下逻辑:
import React from 'react';
import {
PermissionsAndroid,
Platform,
View,
TouchableOpacity,
Text,
} from 'react-native';
import {getAlbums} from '@react-native-community/cameraroll';
const hasAndroidPermission = async () => {
const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE;
const hasPermission = await PermissionsAndroid.check(permission);
if (hasPermission) {
return true;
}
const status = await PermissionsAndroid.request(permission);
return status === 'granted';
};
const handleGetAlbums = async () => {
if (Platform.OS === 'android' && !(await hasAndroidPermission())) {
return;
}
try {
const albums = await getAlbums({assetType: 'All'});
console.log(albums); // returns an object like [{"count": 4, "title": "Downloads"}]
return albums;
} catch (error) {
return error;
}
};
const App = () => {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<TouchableOpacity onPress={handleGetAlbums}>
<Text>Press Me</Text>
</TouchableOpacity>
</View>
);
};
export default App;