Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cordova camera plugin getting real path of selected image

var options = {
    quality: 30, 
    destinationType: Camera.DestinationType.FILE_URI,
    sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
};

navigator.camera.getPicture(
    function(imageURI) {
        window.resolveLocalFileSystemURL(imageURI, function(fileEntry) {
            console.log(fileEntry.toURI());
            scope.$apply(function() {    
                ctrl.$setViewValue(fileEntry.fullPath);
            });
        }, function(err){
            console.log(err);
        }); 
    },
    function(err) {
        console.log(err);
    }, options
);

The imageURI returns '/media/external/images/media/11.

I wanted to get the real path but window.resolveLocalFileSystemURL only returns 'content://media/external/images/media/11'.

I'm trying to get something like '/mnt/sdcard/DCIM/camera/321321321.jpg'.

like image 411
Lionell Briones Avatar asked Oct 20 '22 12:10

Lionell Briones


1 Answers

I just found the solution. The code change should be done within the plugin, not inside the javascript file.

First find the CameraLauncher.java

Add this function. This is the function that will convert '/media/external/images/media/' to realpath

public String getRealPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = cordova.getActivity().getContentResolver().query(contentUri, proj, null, null, null);
    if(cursor.moveToFirst()){;
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

Then find this line. This is the one that returns imageURI on navigator.camera.getPicture(success())

if (this.targetHeight == -1 && this.targetWidth == -1 &&
        (destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) {
    this.callbackContext.success(uri.toString());
}

Change this line to

if (this.targetHeight == -1 && this.targetWidth == -1 &&
        (destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) {
    this.callbackContext.success(getRealPathFromURI(uri)); 
}
like image 144
Lionell Briones Avatar answered Oct 23 '22 04:10

Lionell Briones