import * as MediaLibrary from 'expo-media-library'; // 检查并请求媒体库权限 export const checkMediaLibraryPermission = async (): Promise<{ hasPermission: boolean, status?: string }> => { try { const { status, accessPrivileges } = await MediaLibrary.getPermissionsAsync(); // 如果已经授权,直接返回 if (status === 'granted' && accessPrivileges === 'all') { return { hasPermission: true, status }; } // 如果没有授权,请求权限 const { status: newStatus, accessPrivileges: newPrivileges } = await MediaLibrary.requestPermissionsAsync(); const isGranted = newStatus === 'granted' && newPrivileges === 'all'; if (!isGranted) { console.log('Media library permission not granted or limited access'); } return { hasPermission: isGranted, status: newStatus }; } catch (error) { return { hasPermission: false }; } }; // 获取文件扩展名 export const getFileExtension = (filename: string) => { return filename.split('.').pop()?.toLowerCase() || ''; }; // 获取 MIME 类型 export const getMimeType = (filename: string, isVideo: boolean) => { if (!isVideo) return 'image/jpeg'; const ext = getFileExtension(filename); switch (ext) { case 'mov': return 'video/quicktime'; case 'mp4': return 'video/mp4'; case 'm4v': return 'video/x-m4v'; default: return 'video/mp4'; // 默认值 } };