Search code examples
javascriptcordovawindows-phonecordova-plugins

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


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


Solution

  • 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.