Impossible to pick video from gallery/library - phonegap/cordova

3.3k Views Asked by At

I am using windows phone, cordova/phonegap. How is possible to pick a video from gallery as it was a picture?

The official plugin seems to work for picture only.

Here is the code:

function getVideo() {
    var options = { quality: 80 };
    options["sourceType"] = 0 | 2; 
    options["mediaType"] = 1;
    navigator.camera.getPicture(onVideoSuccess, onFail, options);    
}


var onVideoSuccess = function (fileuri) {
    console.log("fileuri " + fileuri);
}


var onFail = function (err) {
    console.log("onFail");
}

This way I can select the pictures only, shall I change any parameter to select the video files?

Thanks

2

There are 2 best solutions below

2
On BEST ANSWER

It took some time to find it, but it is indeed possible by setting the MediaType.

var pictureSource; 
var destinationType; 
var mediaType;

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    pictureSource = navigator.camera.PictureSourceType;
    destinationType = navigator.camera.DestinationType;
    mediaType = navigator.camera.MediaType;
}

navigator.camera.getPicture(onPhotoURISuccess, onFail, {
    destinationType: destinationType.FILE_URI,
    mediaType: mediaType.VIDEO,
    sourceType: source
});

function onPhotoURISuccess(imageURI) {
    console.log(imageURI);
}

function onFail(message) {
    console.log(message);
}

Found it in this answer: Pick an image / video using PhoneGap in Android More info: http://docs.phonegap.com/en/1.4.0/phonegap_camera_camera.md.html#Camera

FYI:

Camera.MediaType = { 
    PICTURE: 0,             // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType
    VIDEO: 1,               // allow selection of video only, WILL ALWAYS RETURN FILE_URI
    ALLMEDIA : 2            // allow selection from all media types

So in your code, you should change

options["sourceType"] = 0 | 2; 

to:

options["sourceType"] = 1;

to select only videos, or to:

options["sourceType"] = 0;

to select only pictures, or to:

options["sourceType"] = 2; 

to select both.

4
On

This works in Cordova 5.4.1

navigator.camera.getPicture(onSuccess, onFail, { quality: 100,
    destinationType: Camera.DestinationType.FILE_URI,
    sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
    mediaType: Camera.MediaType.VIDEO
  });

Related docs: https://www.npmjs.com/package/cordova-plugin-camera#module_Camera.DestinationType

UPDATE - I take this back, it works for selecting videos in the UI, but I cannot get the FILE_URI in the onSuccess handler to get anything but an empty string, so I can't do anything after they select a video.