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/