Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova 3.4.0: Camera.getPicture() returns encoded URI when selected from GALLERY

I am using Camera.getPicture() API to capture image or select image from GALLERY. When I take the picture using camera, it returns me FileEntry having correct URL with filename and extension. But when I select a file from Gallery, it returns "FileEntry.fullPath" as /com.android.providers.media.documents/document/image%3A322 and sometimes /media/external/images/media/319

What I want is, I want to verify the file type supported(that is jpg/jpeg) and the actual filename.

Is there a way to get the Filename with extension, which is selected.

Thanks in advance.

Code Snippet:

        var data = {};
        if( type === CAMERA){
            data = {
                        quality: quality,
                        destinationType: FILE_URI,
                        encodingType: JPEG, targetWidth: 1200, targetHeight: 1200,
                        saveToPhotoAlbum: true
                    };
        }
        else
        {
            data = {
                        destinationType: FILE_URI,
                        sourceType: PHOTOLIBRARY,
                        mediaType: ALLMEDIA

                    };
        }


        navigator.camera.getPicture(
                successCallback, errorCallback, data
        );   

      //The success callback method is : 
       successCallback: function(imageURI, param)
       {
                 //HERE THE imageURI value is coming with different format if selected from GALLERY
                 window.resolveLocalFileSystemURI(imageURI, 
            function(fileEntry) {fileEntry.file(onSuccess,onError);},
                            function(evt) {onError.call(this,evt.target.error);} );

       }
like image 671
Bidya Avatar asked Jan 10 '23 21:01

Bidya


2 Answers

I was able to convert from a "content://" URI to a "file://" URI using this plugin: https://www.npmjs.com/package/cordova-plugin-filepath.

After obtaining the "file://" URI, I'm then able to use Cordova's resolveLocalFileSystemURL() function.

Hope this helps.

if (fileUri.startsWith("content://")) {
    //We have a native file path (usually returned when a user gets a file from their Android gallery)
    //Let's convert to a fileUri that we can consume properly
    window.FilePath.resolveNativePath(fileUri, function(localFileUri) {
        window.resolveLocalFileSystemURL("file://" + localFileUri, function(fileEntry) {/*Do Something*/});
    });
}
like image 134
Anu2g Avatar answered Jan 13 '23 10:01

Anu2g


in phonegap getpicture method navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] );

we can give cameraOptions

{ quality : 75,
  destinationType : Camera.DestinationType.DATA_URL,
  sourceType : Camera.PictureSourceType.CAMERA,
  allowEdit : true,
  encodingType: Camera.EncodingType.JPEG,
  targetWidth: 100,
  targetHeight: 100,
  popoverOptions: CameraPopoverOptions,
  saveToPhotoAlbum: false };

check the options first.

Android Quirks

Android 4.4 only: Android 4.4 introduced a new Storage Access Framework that makes it easier for users to browse and open documents across all of their preferred document storage providers. Cordova has not yet been fully integrated with this new Storage Access Framework. Because of this, the getPicture() method will not correctly return pictures when the user selects from the "Recent", "Drive", "Images", or "External Storage" folders when the destinationType is FILE_URI. However, the user will be able to correctly select any pictures if they go through the "Gallery" app first. Potential workarounds for this issue are documented on this StackOverflow question. Please see CB-5398 to track this issue.

Android uses intents to launch the camera activity on the device to capture images, and on phones with low memory, the Cordova activity may be killed. In this scenario, the image may not appear when the Cordova activity is restored.

If you got fileEntry you can use file() method to get mettadatas

 function cameraSuccess(urls) {

       window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(){

                    // alert('success requestFileSystem');

                 }, function(){
                 //error

                   });

        window.resolveLocalFileSystemURI(urls, function(fileEntry){


                   fileEntry.file(function(file){

                             // alert(JSON.stringify(file)); //view full metadata
                                var type = file.type;
                                var nameoffile = file.name;

                               }, function(){

                                //error                                                 
                                });

                  },function(){

                 // error 
                  } ); 

One Last option is to create one custom plugin for finding type of image

like image 20
Arjun T Raj Avatar answered Jan 13 '23 11:01

Arjun T Raj