Open History to see the last commit
1import { requireNativeModule } from "expo";
2import { Platform } from "react-native";
3
4const LIBRARY_IMAGE_MIME_TYPE = /^image\/(png|jpe?g|gif|webp|heic|heif)$/i;
5// Photos only takes the containers AVFoundation reads; Android's MediaStore takes any of these.
6const LIBRARY_VIDEO_MIME_TYPE =
7 Platform.OS === "ios"
8 ? /^video\/(mp4|quicktime|x-m4v)$/i
9 : /^video\/(mp4|quicktime|3gpp|webm|x-matroska)$/i;
10
11// MIME types are case-insensitive; the native modules pick video or image by prefix.
12function baseMimeType(mimeType: string): string {
13 return mimeType.split(";", 1)[0]?.trim().toLowerCase() ?? "";
14}
15
16// Android 9 and older need a storage permission to add media, so they keep the share sheet.
17export function mediaLibraryKind(mimeType: string): "image" | "video" | null {
18 if (Platform.OS !== "ios" && !(Platform.OS === "android" && Platform.Version >= 29)) return null;
19 const type = baseMimeType(mimeType);
20 if (LIBRARY_IMAGE_MIME_TYPE.test(type)) return "image";
21 if (LIBRARY_VIDEO_MIME_TYPE.test(type)) return "video";
22 return null;
23}
24
25/** Adds a local image or video to Photos on iOS, or to Pictures or Movies/T3 Code on Android. */
26export function saveToMediaLibrary(uri: string, mimeType: string, name: string): Promise<void> {
27 return requireNativeModule<{
28 saveToMediaLibrary(uri: string, mimeType: string, name: string): Promise<void>;
29 }>("T3NativeControls").saveToMediaLibrary(uri, baseMimeType(mimeType), name);
30}