Cordova camera plugin gets the real path of the 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
);

ImageURI returns' / media / external / images / media / 11.

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

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

+4
source share
5 answers

I have found a solution. Changing the code should be done inside the plugin and not inside the javascript file.

First find CameraLauncher.java

Add this feature. This is a function that converts '/ 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;
}

. , imageURI navigator.camera.getPicture(success())

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

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

fileEntry.toURL()

0

. cordova-plugin-filepath. .

    $cordovaCamera.getPicture(options).then(function(imageUrl) {
//FilePath will resolve the path
        window.FilePath.resolveNativePath(imageUrl, function (result) {
            imageURI = 'file://' + result;
            console.log(imageURI);
            resolve(imageURI);
        });
    });

edit (allowEdit: true) . !

0
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,destinationType:Camera.DestinationType.FILE_URI,mediaType:Camera.MediaType.ALLMEDIA,encodingType:Camera.EncodingType.JPEG,saveToPhotoAlbum:true,sourceType: Camera.PictureSourceType.PHOTOLIBRARY});
function onSuccess(imageData) {
    $("#img_id").attr("src",imageData);
    alert(imageData);
}

/storage/sdcard0/Pictures/IMG_9999433.jpg

0

function getImageURI(imageURI) {

var gotFileEntry = function(fileEntry) {
    alert("got image file entry: " + fileEntry.fullPath);
    var gotFileSystem = function(fileSystem) {
// your code 
-2

Source: https://habr.com/ru/post/1532603/


All Articles