tiefen_space_app/utils/saveVideo.js

37 lines
1017 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import * as MediaLibrary from "expo-media-library";
import * as FileSystem from "expo-file-system";
import Toast from "react-native-toast-message";
export default async function saveVideo(uri, callback = () => {}) {
const permission = await MediaLibrary.requestPermissionsAsync();
if (permission.granted) {
const timestamp = new Date().getTime();
const downloadResumable = FileSystem.createDownloadResumable(
uri,
FileSystem.cacheDirectory + `${timestamp}.mp4`,
{},
callback
);
try {
const res = await downloadResumable.downloadAsync();
await MediaLibrary.saveToLibraryAsync(res.uri);
Toast.show({
type: "success",
text1: "已保存到相册",
topOffset: 60,
});
return true;
} catch (err) {
console.error("FS Err: ", err);
return false;
}
} else {
Toast.show({
type: "error",
text1: "保存失败请检查APP媒体权限",
topOffset: 60,
});
return false;
}
}