我正在尝试使用离子Cordova文件传输iOS设备中的URL保存视频。
var fileTransfer = new FileTransfer();
var uri = "https:xyz.abc/demo.mp4";
var fileURL = cordova.file.documentsDirectory + 'demoRename.mp4';
fileTransfer.download(
uri, fileURL, function (entry) {
console.log("download complete: " + entry.toURL());
},
function (error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("download error code" + error.code);
}
);
我可以使用控制台日志看到下载的路径。
[Log] download complete: file:///var/mobile/Containers/Data/Application/EB4C2F9A-9C4D-4CA6-BED0-D83B7D4CBDE1/Documents/demoRename.mp4
但是我在画廊或照片中看不到下载的视频文件,我在哪里可以找到它们或做对了?
您需要将视频保存到画廊,然后您可以在画廊中看到下载的视频。
请使用照片库插件并点击以下链接。
https://github.com/terikon/cordova-plugin-photo-library
使用以下代码可能对您有所帮助。
//based on device type choosing the location to save the video
if (ionic.Platform.isIOS()) {
var path = cordova.file.documentsDirectory;
var filename = "preview.mp4";
} else {
var path = cordova.file.externalRootDirectory;
var filename = "preview" + $filter('date')(new Date(), "yyyy-MM-dd HH:mm:ss") + ".mp4";
}
//Creating directory to save video in the device
$cordovaFile.createDir(path, "dir", true).then(function(success) {
var targetPath = path + "dir/" + filename;
var trustHosts = true;
var options = {};
//Downloading the file from URL.
$cordovaFileTransfer.download("video URL", targetPath, options, trustHosts).then(function(result) {
//Once downloaded the file calling function to add the video to photo library
if (ionic.Platform.isIOS()) {
function savelibrery() {
cordova.plugins.photoLibrary.saveVideo(targetPath, album, function(success) {}, function(err) {
if (err.startsWith('Permission')) {
cordova.plugins.photoLibrary.requestAuthorization(function() {
savelibrery();
}, function(err) {
$ionicLoading.hide();
$cordovaToast.show("Oops! unable to save video, please try after sometime.", "long", "center");
// User denied the access
}, // if options not provided, defaults to {read: true}.
{
read: true,
write: true
});
}
});
}
savelibrery()
} else {
//add refresh media plug in for refresh the gallery for android to see the downloaded video.
refreshMedia.refresh(targetPath);
}
$ionicLoading.hide();
$cordovaToast.show("Vidoe saved successfully", "long", "center");
}, function(error) {
$ionicLoading.hide();
$cordovaToast.show("Oops! unable to save video, please try after sometime.", "long", "center");
}, function(progress) {
$scope.downloadProgress = (progress.loaded / progress.total) * 100;
$cordovaToast.show("In progress", "short", "center");
});
}, function(error) {
//alert(JSON.stringify(error));
$ionicLoading.hide();
$cordovaToast.show("Oops! unable to save video, please try after sometime.", "long", "center");
});