2024-03-15 20:14:22 +08:00
|
|
|
|
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 saveImage(uri) {
|
|
|
|
|
const permission = await MediaLibrary.requestPermissionsAsync();
|
|
|
|
|
if (permission.granted) {
|
|
|
|
|
const timestamp = new Date().getTime();
|
|
|
|
|
const fileUri = FileSystem.cacheDirectory + `${timestamp}.png`;
|
2024-03-23 15:29:03 +08:00
|
|
|
|
try {
|
|
|
|
|
const res = await FileSystem.downloadAsync(uri, fileUri);
|
|
|
|
|
await MediaLibrary.saveToLibraryAsync(res.uri);
|
|
|
|
|
Toast.show({
|
|
|
|
|
type: "success",
|
|
|
|
|
text1: "已保存到相册",
|
|
|
|
|
topOffset: 60,
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("FS Err: ", err);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-03-15 20:14:22 +08:00
|
|
|
|
} else {
|
|
|
|
|
Toast.show({
|
|
|
|
|
type: "error",
|
|
|
|
|
text1: "保存失败,请检查APP媒体权限",
|
|
|
|
|
topOffset: 60,
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|