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

笑着哭i 提交于 2020-01-17 01:39:07

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/30753364/impossible-to-pick-video-from-gallery-library-phonegap-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!