62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import * as MediaLibrary from 'expo-media-library';
|
|
|
|
export type MediaItem = {
|
|
id: string;
|
|
uri: string;
|
|
creationTime: number;
|
|
filename: string;
|
|
};
|
|
|
|
// 获取指定时间范围内的图片
|
|
export const getFilteredMedia = async (range: 'today' | 'week' | 'month' | 'all'): Promise<MediaItem[]> => {
|
|
const { status } = await MediaLibrary.requestPermissionsAsync();
|
|
if (status !== 'granted') throw new Error('Permission not granted');
|
|
console.log("statusq111111111111111111", status);
|
|
|
|
let cutoffDate: Date;
|
|
|
|
switch (range) {
|
|
case 'today':
|
|
cutoffDate = new Date();
|
|
cutoffDate.setHours(0, 0, 0, 0);
|
|
break;
|
|
case 'week':
|
|
cutoffDate = new Date();
|
|
cutoffDate.setDate(cutoffDate.getDate() - 7);
|
|
break;
|
|
case 'month':
|
|
cutoffDate = new Date();
|
|
cutoffDate.setMonth(cutoffDate.getMonth() - 1);
|
|
break;
|
|
default:
|
|
cutoffDate = new Date(0); // 所有图片
|
|
}
|
|
console.log("cutoffDateq111111111111111111", cutoffDate);
|
|
|
|
const albums = await MediaLibrary.getAlbumsAsync({ includeSmartAlbums: true });
|
|
console.log("albumsq111111111111111111", albums);
|
|
let allAssets: MediaItem[] = [];
|
|
|
|
for (const album of albums) {
|
|
const result = await MediaLibrary.getAssetsAsync({
|
|
album: album.id,
|
|
mediaType: ['photo'],
|
|
first: 1000,
|
|
});
|
|
console.log("result111111111111", result);
|
|
|
|
const filtered = result.assets
|
|
.filter(asset => asset.creationTime > cutoffDate.getTime())
|
|
.map(asset => ({
|
|
id: asset.id,
|
|
uri: asset.uri,
|
|
creationTime: asset.creationTime,
|
|
filename: asset.filename,
|
|
}));
|
|
console.log("filtered111111111111", filtered);
|
|
|
|
}
|
|
|
|
console.log("allAssetsq111111111111111111", allAssets);
|
|
return allAssets;
|
|
}; |