原因: 插件源码使用的CameraRoll已从RN移除,需要另外添加@react-native-community/cameraroll 使用ImageViewer提供的onSave回调实现保存:
import path from 'react-native-path';
import RNFetchBlob from 'rn-fetch-blob';
import CameraRoll from "@react-native-community/cameraroll";
import ImageViewer from 'react-native-image-zoom-viewer';
const onSave = async (url) => {
let localUri = url;
if (Platform.OS === 'android') {
if (await hasAndroidPermission()) {
let fileName = path.basename(url);
// 文件夾路徑
let dirPath = RNFetchBlob.fs.dirs.CacheDir + '/imgs';
// 完整路徑
let imagePath = path.fullpath(dirPath, fileName);
RNFetchBlob.config({ path: imagePath })
.fetch('GET', url, {})
.then((res) => {
localUri = res.path().startsWith('file://') ? res.path() : `file://${res.path()}`;
saveImage(localUri);
})
.catch((err) => {
console.warn('download err', err);
});
}
} else {
saveImage(localUri);
}
};
const saveImage = (url) => {
CameraRoll.save(url).catch((err) => {
console.warn('save err', err);
});
}
<ImageViewer
......
onSave={onSave}
注意: @react-native-community/cameraroll提供的save方法需要的url必须是本地链接,可以用rn-fetch-blob先将图片下载
CameraRoll.save(tag, { type, album }) On Android, the tag must be a local image or video URI, such as “file:///sdcard/img.png”.
|