简体   繁体   English

Cordova相机插件,从图库[ANDROID]获取完整的图像路径

[英]Cordova camera plugin, obtain full image path from gallery [ANDROID]

I am trying to get an image from the gallery with cordova camera plugin, this is the way I am doing it: 我正试图从带有cordova相机插件的画廊中获取图像,这就是我这样做的方式:

navigator.camera.getPicture(onSuccess, onFail, {
  quality: 50,
  sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
  destinationType: Camera.DestinationType.FILE_URI,
  mediaType: Camera.MediaType.ALLMEDIA,
  encodingType: Camera.EncodingType.JPEG
});

But in my 'onSuccess' function I get the image URI in the format: 但是在我的'onSuccess'函数中,我得到了以下格式的图像URI:

"content://com.android.providers.media.documents/document/image%3A1509"

And I want the URI with the full path to the image, just like I get it if I use the camera instead: 我希望URI具有图像的完整路径,就像我在使用相机时获取它一样:

"file:///storage/emulated/0/Pictures/IMG_20150710_124222.jpg"

Reading the official documentation I have seen that the only thing I am supposed to do is to set the property 'DestinationType' to 'FILE_URI', which is already set as you can see above. 阅读官方文档我已经看到,我应该做的唯一事情是将属性'DestinationType'设置为'FILE_URI',已经设置,如上所示。

What am I doing wrong? 我究竟做错了什么?

I have found a plugin that converts the URI in 'content://...' format into full file path, it isn't the perfect solution but it is the one has worked for me, the plugin is cordova-plugin-filepath , I use it in the onSuccess callback for the getCamera function: 我找到了一个插件,将'content:// ...'格式的URI转换为完整的文件路径,它不是完美的解决方案,但它是一个为我工作的插件,插件是cordova-plugin-filepath ,我在getCamera函数的onSuccess回调中使用它:

function onSuccess(imageURI) {
  window.FilePath.resolveNativePath(imageURI, function(result) {
    // onSuccess code
    imageURI = 'file://' + result;
    . . .
  }, function (error) {
    // onError code here
  }
}

You can also do it using single button with the help of confirm box : 您也可以在确认框的帮助下使用单个按钮执行此操作:

var txt=confirm("Select photo source");
if(txt==true){
         navigator.camera.getPicture(onSuccess, onFail, { quality: 50,destinationType: Camera.DestinationType.DATA_URL,saveToPhotoAlbum: true,sourceType: Camera.PictureSourceType.CAMERA});
}
else{
    navigator.camera.getPicture(onSuccess, onFail, { quality: 50,destinationType: Camera.DestinationType.DATA_URL,saveToPhotoAlbum: true,sourceType: Camera.PictureSourceType.PHOTOLIBRARY});
}

If ok,then camera opens else if you click cancel, then galery opens 如果没问题,那么如果您单击取消,相机会打开,然后打开galery

you can also customize ok-cancel button of confirm botton according to your need. 您还可以根据需要自定义确认按钮的确定取消按钮。

I guess The problem in your code is here 我想你的代码中的问题就在这里

mediaType: Camera.MediaType.ALLMEDIA,

either remove this line or make it 要么删除此行,要么删除它

 mediaType: Camera.MediaType.PICTURE,

happy coding.... 快乐的编码....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM