使用Cordova插件在iOS库中保存视频文件

时间:2017-03-09 13:44:10

标签: angularjs cordova ionic-framework ionic2 cordova-plugins

我正在尝试使用离子Cordova file transfer在我的iOS设备中保存网址中的视频。

  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 

但是我无法在我的图库或照片中看到下载的视频文件,我在哪里可以找到它们,或者我做得对吗?

1 个答案:

答案 0 :(得分:0)

您需要将视频保存到图库,然后您才能在图库中看到下载的视频。

请使用照片库插件,然后按照以下链接。

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");
 });