【问题标题】:How can I retrieve an image which is saved in the photolibrary of an android system?如何检索保存在 android 系统的 photolibrary 中的图像?
【发布时间】:2012-03-01 04:06:50
【问题描述】:

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

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

它在“fileSystem.root.getFile”调用处停止-(错误回调中的错误:File4 = TypeError:表达式'evt.target'[未定义]的结果不是对象。在文件:///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);}}

【问题讨论】:

    标签: cordova base64 photolibrary


    【解决方案1】:

    我发现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 的事情;你可以创建一个 FileReader,因为你有完全限定的 URI。

    这在 iOS 上对我有用。我将很快测试 Android,并将返回我的结果。

    【讨论】:

      【解决方案2】:

      我不知道你为什么需要 getPicture 来返回 URI 和 Data。

      如果需要显示图片可以让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数据并显示图片

      【讨论】:

      • 这在 iphone 和 blackberry 中不起作用..它只返回文件 uri 而不是 base 64 编码字符串 ...
      【解决方案3】:

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

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

      让我们知道这会产生什么...

      【讨论】:

      • 他的代码直接来自PhoneGap的API。但是 JSON.stringify 只返回 { "code" : X}。所以你应该记录 evt.code。
      • ??我的意思是他不应该记录/警告 evt.target.error.code 而只是 evt.code..
      • 我更喜欢对整个对象进行字符串化,因为其中可能还有其他相关信息..
      猜你喜欢
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 2020-09-06
      • 2019-10-03
      • 1970-01-01
      • 2016-04-14
      相关资源
      最近更新 更多