Search code examples
angularjscordovaionic-frameworkcordova-media-plugin

Video capture to not show in my gallery on phone


I am using ng-cordova's plugin capture to capture (https://github.com/apache/cordova-plugin-media-capture) videos inside of the ionic framework from a mobile phone.

$scope.captureVideo = function() {
var options = { limit: 1, duration: 10 };

    $cordovaCapture.captureVideo(options).then(function(videoData) {
        var i, path, len;
        for (i = 0, len = videoData.length; i < len; i += 1) {
            path = videoData[i].fullPath;
            console.log("Path of the video is = " + path.toString());
        }
    }, function(err) {
        // An error occurred. Show a message to the user
    });
}

The problem is every video capture I take saves to my phones gallery which I do not want. If I capture an image it does NOT save to my phone's gallery which is what I would like with video capture. Is there a way to stop videos from being saved?

I tried deleting the file but apparently the video file is not saved in cordovafile directories.

function DeleteFile() {

var filename = "20161024_095758.mp4";
var relativeFilePath = "file:/storage/C8F0-1207/DCIM/Camera";
console.log('data directory: '+cordova.file.dataDirectory);
window.resolveLocalFileSystemURL(relativeFilePath, function(dir) {
dir.getFile(filename, {create:false}, function(fileEntry) {
              fileEntry.remove(function(){
                alert('file removed');
                  // The file has been removed succesfully
              },function(error){
                alert('error'+JSON.stringify(error));
                  // Error deleting the file
              },function(){
                alert('file doesnt exist');
                 // The file doesn't exist
              });
  });
});

The code above to delete file results in Error Code:6. No modification allowed


Solution

  • Well, apparently WE CANNOT DELETE AND WRITE FILES FROM MICRO SD-CARD SINCE VERSION 4.4. It is read only now.. And, when the user deletes the file from the gallery, it would not be available for the project. Here is what i came up with.

    I copied the video file to cordova's external directory from where i could read the file when wanted and delete as per the need. Plugins required are cordova-file-plugin and cordova-plugin-file-transfer

    .controller('yourCtrl', function($scope,$cordovaCapture,$sce,$cordovaFile, $cordovaFileTransfer, $timeout) {
           $cordovaCapture.captureVideo(options).then(function(videoData) {
                console.log(JSON.stringify(videoData[0]));
                console.log(cordova.file.externalDataDirectory);
                 $cordovaFileTransfer.download(videoData[0].fullPath, cordova.file.externalDataDirectory + 'my-video.mp4', {}, true).then(
                                    function(result)
                                    {
                                        console.log('success: '+ result);
                                    },
                                    function (error)
                                    {
                                        console.log('error: '+ JSON.stringify(error));
                                    },function (progress) {
                                        $timeout(function () {
                                          $scope.downloadProgress = (progress.loaded / progress.total) * 100;
                                        });
                                      },false);
                   $scope.clipSrc = $sce.trustAsResourceUrl(videoData[0].fullPath);
                   //$scope.videoSrc = videoData[0].fullPath;
            }, function(err) {
              alert('Err: <br />'+ JSON.stringify(videoData));
            });
    
      //delete the file according to filename.
    
        $scope.deleteVideo= function(){
            $cordovaFile.removeFile(cordova.file.externalDataDirectory, "my-video.mp4")
              .then(function (result) {
                console.log('Success: deleting videoData file' + JSON.stringify(result));
              }, function (err) {
                console.log('Error: deleting videoData file' + JSON.stringify(err));
              });
          }
    
    })