我创建了通过
rn-fetch-blob
库将视频从 url 下载到本地存储的功能,它在 Android 上运行良好,但在 ios 上不起作用
我通过 xcode 添加了权限,但是当我打开照片或相机胶卷时,我找不到视频
async download() {
this.setState({btnDownloadLoading: true});
const {video} = this.state;
let hasPermission = await this.requestStoragePermission();
console.log('hasPermission : ', hasPermission);
if (!hasPermission) {
this.requestStoragePermission();
this.setState({btnDownloadLoading: false});
return false;
}
var date = new Date();
var url = video.video_url;
var ext = this.extention(url);
ext = "." + ext[0];
const {config, fs} = RNFetchBlob
let MovieDir = fs.dirs.DownloadDir
let options = {
fileCache: true,
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
path: MovieDir + "/video_" + Math.floor(date.getTime() + date.getSeconds() / 2) + ext,
description: 'Video'
}
}
config(options).fetch('GET', url).then((res) => {
this.setState({btnDownloadLoading: false});
Alert.alert("تم تحميل الفيديو بنجاح.");
});
}
我需要适用于 ios 和 android 的代码,将视频从 url 下载到我的手机
我正在从 ip 摄像机下载视频,并将其保存到照片中(在 Android 上运行良好),但 Ios 显示错误: “操作无法完成(PHPhotosErrorDomain 错误 3302)”
这是我下载视频的代码:
const handleDownload = async filePath => {
const videoPath = filePath.split('A:\\')[1].replace(/\\/g, '/');
const vUrl = `http://${selectedCamera.ip}/${videoPath}`;
const {config, fs} = RNFetchBlob;
const downloads =Platform.OS === 'ios' ? fs.dirs.DocumentDir
fs.dirs.DownloadDir;
const tsFilePath = `${downloads}/${videoPath.split('/').pop()}`;
const mp4FilePath = tsFilePath.replace('.TS', '.mp4');
// Check if the video is already downloaded
const storedDownloads =
JSON.parse(await AsyncStorage.getItem('downloads')) || [];
const isAlreadyDownloaded = storedDownloads.some(
video => video.filePath === mp4FilePath,
);
if (isAlreadyDownloaded) {
Alert.alert(
'Already Downloaded',
'This video has already been downloaded.',
);
return; // Exit the function early
}
// If not downloaded, proceed with download
setTimeout(() => dispatch(DownloadLoader()), 700);
try {
const response = await config({
fileCache: true,
path: tsFilePath,
})
.fetch('GET', vUrl)
.progress((received, total) => {
const progress = received / total;
setDownloadProgress(progress); // Update progress
});
await FFmpegKit.execute(`-i ${tsFilePath} -c copy ${mp4FilePath}`);
await fs.unlink(tsFilePath);
/*saving the file in albums */
let savedUri = null;
try {
savedUri = await CameraRoll.save(mp4FilePath, {type: 'video'});
Alert.alert(
'Saved to Photos ',
'Video has been saved to your Phone Album',
);
console.log(savedUri);
} catch (saveError) {
Alert.alert(
'Save Failed',
`Failed to save video to Photos: ${saveError.message}`,
);
}
const downloadedVideo = {
name: mp4FilePath.split('/').pop(),
filePath: savedUri,
url: vUrl,
};
dispatch(addToDownloads(downloadedVideo));
storedDownloads.push(downloadedVideo);
await AsyncStorage.setItem('downloads', JSON.stringify(storedDownloads));
console.log(downloadedVideo);
} catch (error) {
Alert.alert('Download Failed', `Error: ${error.message}`);
} finally {
setTimeout(() => dispatch(DownloadLoader(false)), 700);
}
};
问题不在于文件扩展名,而是 (.mp4),也在 Android 上检查过 我也在 info.plist 中授予了必要的权限,但我仍然在 ios 上收到此错误
您可以使用相机胶卷来保存图像。
CameraRoll.saveToCameraRoll(filePath, 'photo')
.then(Alert.alert('Success', ' added to camera roll!'))
它对我有用。