【问题标题】:save video files in iOS gallery using Cordova plugin使用 Cordova 插件将视频文件保存在 iOS 库中
【发布时间】:2017-03-09 13:44:09
【问题描述】:

我正在尝试使用 ionic Cordova file transfer 从我的 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 

但是我在我的画廊或照片中看不到下载的视频文件,我在哪里可以找到它们或者我做对了吗?

【问题讨论】:

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


    【解决方案1】:

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

    请使用照片库插件并点击以下链接。

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

    【讨论】:

    • 我得到一个 refreshMedia 没有定义。我在这里缺少插件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 2017-10-31
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多