Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cordova 如何检索保存在android系统照片库中的图像?_Cordova_Base64_Photolibrary - Fatal编程技术网

Cordova 如何检索保存在android系统照片库中的图像?

Cordova 如何检索保存在android系统照片库中的图像?,cordova,base64,photolibrary,Cordova,Base64,Photolibrary,我正在尝试访问android系统的照片库来检索图像。我有navigator.camera.getPicture函数提供的imageURI变量。在那之前没关系。但稍后,我想访问photolibrary并获取此图像的base64代码 由于navigator.camera.getPicture不可能同时返回两个数据(imageURI和imageData),因此我需要稍后获取base64信息。下面是我试图使用的代码,查看phoneGap的“文件”文档,但它不起作用 它在“fileSystem.root.

我正在尝试访问android系统的照片库来检索图像。我有navigator.camera.getPicture函数提供的imageURI变量。在那之前没关系。但稍后,我想访问photolibrary并获取此图像的base64代码

由于navigator.camera.getPicture不可能同时返回两个数据(imageURI和imageData),因此我需要稍后获取base64信息。下面是我试图使用的代码,查看phoneGap的“文件”文档,但它不起作用

它在“fileSystem.root.getFile”调用时停止-(错误回调中的错误:File4=TypeError:expression'evt.target'[undefined]的结果不是对象。在file:///android_asset/www/phonegap-1.3.0.js:717)

谁能帮我?谢谢

    function base64(imageURI) {
 alert(imageURI);
 document.addEventListener("deviceready", onDeviceReady);

 function onDeviceReady() {
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);}

 function gotFS(fileSystem) {
     alert("filesystem");
             //Next line causes error. Perhaps imageURI is not a valid path?
     fileSystem.root.getFile(**imageURI**, null, gotFileEntry, fail);}


 function gotFileEntry(fileEntry) {
     alert("gotfileentry");
     fileEntry.file(gotFile, fail);}

 function gotFile(file){
     alert("got file");
     readDataUrl(file);}

 function readDataUrl(file) {
     alert("readDataURL");
     var reader = new FileReader();
     reader.onloadend = function(evt) {
         console.log("Read as data URL");
         alert(evt.target.result);
         };
     reader.readAsDataURL(file);
 }
 function fail(evt) {
     console.log(evt.target.error.code);}}

好的,第一个问题是fail函数返回的对象没有您试图访问的属性。要查看错误对象中的内容,请尝试以下操作:

 function fail(evt) {
                 alert("there was an error: " + JSON.stringify(evt));
            }

让我们知道它产生了什么…

我不知道为什么需要getPicture来返回URI和数据

如果需要显示图片,可以要求getPicture返回base 64中的数据:

    function capturePhoto(){
        navigator.camera.getPicture(
            addPictureToActuSuccess, 
            addPictureToActuFail, {
                quality: 50,
                destinationType: navigator.camera.DestinationType.DATA_URL,
                sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
            }
        );
    }

    function addPictureToActuSuccess(data){
        // Store the base64 data in a file or in a variable
    }

    function addPictureToActuFail(error){
        console.log(error);
    }
并用css显示:

url('data:image/png;base64,BASE64_DATA') /* From where you stored you base64 data */

这样,您还可以存储base 64数据并显示图片

我发现从API返回的imageURI必须从文件协议中删除:

var rf=app.profile.customer.site.imageURI.substring(16); // strip off file://localhost
var reader=new FileReader();
reader.onloadend=function(evt){
  // the image data is in :  evt.target.result
  // it starts with:  data:image/jpeg;base64,/9j/....
  // so you may need to strip off the first 24 chars
}
reader.readAsDataURL(rf);
同样,如上所述,您不需要做FileSystem/FileEntry/File的事情;您可以创建一个文件阅读器,因为您拥有完全限定的URI


这在iOS上对我有效。我将很快测试Android,并将公布结果。

他的代码直接来自PhoneGap的API。但是JSON.stringify只返回{“code”:X}。所以你应该记录evt.code。??我的观点是他不应该记录/提醒evt.target.error.code,而应该只记录evt.code..我更喜欢字符串化整个对象,因为其中可能有其他相关信息..这在iphone和blackberry中不起作用..它只返回文件uri,而不是base 64编码的字符串。。。