android - cordova 相机插件获取所选图像的真实路径

标签 android angularjs cordova

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/”转换为真实路径的函数

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(success()) 上返回 imageURI 的那个

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)); 
}

关于android - cordova 相机插件获取所选图像的真实路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22524410/

相关文章:

android - 如何在android中的firebase中创建唯一的id

android - 符合 Google Play 政策的移动广告

javascript - JsPlumb 端点覆盖

android - ionic 请求仅在 android 上返回 404,在 Chrome 中它工作正常

android - 如何在 Android SDK 示例中的 MapViewCompassDemo 中管理叠加层

javascript - 使用 ng-repeat 标签和 json 数据时

javascript - 正则表达式不匹配内容,而是匹配整个搜索字符串

javascript - Apache Cordova 应用程序未更新

Cordova/PGB 应用在恢复后卡住

java - Android 无法扩展 Firebase 消息服务