簡體   English   中英

科爾多瓦相機插件獲得所選圖像的真實路徑

[英]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
);

imageURI返回'/ media / external / images / media / 11。

我想獲得真正的路徑,但window.resolveLocalFileSystemURL只返回'content:// media / external / images / media / 11'。

我想要得到像'/mnt/sdcard/DCIM/camera/321321321.jpg'這樣的東西。

我剛剛找到了解決方案。 代碼更改應該在插件中完成,而不是在javascript文件中。

首先找到CameraLauncher.java

添加此功能。 這是將'/ media / external / images / media /'轉換為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;
}

然后找到這一行。 這是在navigator.camera.getPicture上返回imageURI的那個(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)); 
}

你必須使用fileEntry.toURL()

我也在努力解決這個問題。 您必須使用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);
        });
    });

有了這個,你可以啟用編輯(allowEdit:true)甚至保存你的photolibrary。 經過測試和工作!

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

你可以用它

function getImageURI(imageURI) {

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM