Saturday, August 13, 2016

Cordova Camera Plugin Select Video Returns null FILE URI


Recently in one of my project we have a requirement to select video from Gallery and import it to Cordova application.

For that I used following code., function(){
            console.log('Error in Video Processing);
        }, {
            destinationType: Camera.DestinationType.FILE_URI,
            limit: 1

As you can see in above code we are selecting media type to video. Following is our callback function.

    onVideoSuccess: function(videoURI){

Now the problem was it was always returns null URI for any video I select. So I decided to dig into plugin code and found issue in Following is the function.

private void processResultFromGallery(int destType, Intent intent) {
 Uri uri = intent.getData();
        if (uri == null) {
            if (croppedUri != null) {
                uri = croppedUri;
            } else {
                this.failPicture("null data from photo library");
Here we have issue in following line.

        String fileLocation = FileHelper.getRealPath(uri, this.cordova);

This always returns null, however we were getting URI. So I added following code. 

if (this.mediaType != PICTURE) {
         if (fileLocation == null) {
         Cursor cursor = null;
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = this.webView.getContext().getContentResolver().query(uri,  proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            fileLocation = cursor.getString(column_index);
          } finally {
            if (cursor != null) {

and it worked.  Now it returned correct URI.

Hope this helps you.

No comments:

Post a Comment