2014-05-19 31 views
1

在我的phonegap应用程序(android版本4.4.2)中,我需要选择图像形式的sdcard。在这我不能读取图像大小和名称。我的代码就像。从图像专辑中选择图像时文件大小不能读取

function getSkiImage(id,source){ 
    navigator.camera.getPicture(function(imageURI){ 
     alert(imageURI); 
     window.resolveLocalFileSystemURI(imageURI, function(fileEntry) { 
      alert(fileEntry); 
      fileEntry.file(function(fileObj) { 
       alert(fileObj.size); 

      }); 
     }); 


// tried this also 
/*window.requestFileSystem(imageURI, 0, function(data) { 
    alert(data.size); 

}, fail); */ 


}, fail, { quality: 50, 
    destinationType: destinationType.FILE_URI, 
    sourceType: pictureSource.PHOTOLIBRARY }); 
} 

在我的Android设备(V 4.4.2)的专辑节目,如“最近”,“驱动器”,“图像”,“画廊”,......当从图库中选择图像,然后只图像尺寸为比画廊图像大小getting..other是不是能够得到..

refered这一点,但没有获得成功

Cordova/PhoneGap Photo File Size

在PhoneGap的文档,他们说:

仅适用于Android 4.4:Android 4.4引入了新的存储访问 使用户能够更轻松地浏览和打开所有首选文档存储提供商的文档 。 Cordova有 还没有完全集成到这个新的存储访问框架中。 因此,当destinationType为FILE_URI时,用户从“最近”,“驱动器”,“图像”, 或“外部存储”文件夹中进行选择时,getPicture()方法将不会正确返回 图片。 但是,如果他们首先通过“图库”应用程序 ,用户将能够正确选择任何图片。此StackOverflow问题中记录了 此问题的潜在解决方法。请参阅 CB-5398来跟踪此问题。

Android使用意图将设备上的相机活动启动为 捕捉图像,而在低内存手机上,科尔多瓦活动 可能会死亡。在这种情况下,当恢复Cordova活动时,图像可能不会显示。

+0

它显示什么错误?你看过科尔多瓦的File插件吗? https://cordova.apache.org/docs/en/3.0.0/cordova_file_file.md.html – smj2393

+0

@ smj2393没有显示任何内容... – Vini

+0

问题,你想在真实的设备上测试这个,或者你在尝试它吗?模拟器? – Gajotres

回答

1
<!DOCTYPE html> 
<html> 
    <head> 
    <title>Capture Photo</title> 

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script> 
    <script type="text/javascript" charset="utf-8"> 

    var pictureSource; // picture source 
    var destinationType; // sets the format of returned value 

    // Wait for PhoneGap to connect with the device 
    // 
    document.addEventListener("deviceready",onDeviceReady,false); 

    // PhoneGap is ready to be used! 
    // 
    function onDeviceReady() { 
     pictureSource=navigator.camera.PictureSourceType; 
     destinationType=navigator.camera.DestinationType; 
     getPhoto(pictureSource.PHOTOLIBRARY); 
    } 


    function onPhotoURISuccess(imageURI) { 
     alert(imageURI); 

     var largeImage = document.getElementById('largeImage'); 

     largeImage.style.display = 'block'; 


     largeImage.src = imageURI; 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, rfail); 


     window.resolveLocalFileSystemURI(imageURI, onResolveSuccess, fail); 
    } 

    function rfail(e){ 
     alert(e); 
    } 
    function getPhoto(source) { 
     // Retrieve image file location from specified source 
     navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
     destinationType: destinationType.FILE_URI, 
     sourceType: source }); 
    } 
    function onFileSystemSuccess(fileSystem) { 
     console.log(fileSystem.name); 
    } 
    function bytesToSize(bytes) { 
     var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; 
     if (bytes == 0) return 'n/a'; 
     var i = parseInt(Math.floor(Math.log(bytes)/Math.log(1024))); 
     return Math.round(bytes/Math.pow(1024, i), 2) + ' ' + sizes[i]; 
    }; 
    function onResolveSuccess(fileEntry) { 


     filenameofajax=fileEntry.name; 

     var efail = function(evt) { 
      console.log("File entry error "+error.code); 
     }; 
     var win=function(file) { 
      console.log(file); 
      for (var i in file){ 
       alert(i+""+file[i]); 
      } 
      alert(bytesToSize(file.size)); 

     }; 
     fileEntry.file(win, efail); 
    } 
    function efail(e) { 
     alert("esa") 
    } 
    function fail(e) { 
     alert("sa") 
    } 

    // Called if something bad happens. 
    // 
    function onFail(message) { 
     alert('Failed because: ' + message); 
    } 

    </script> 
    </head> 
    <body> 
    <button onclick="capturePhoto();">Capture Photo</button> <br> 
    <button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br> 
    <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br> 
    <button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br> 
    <img style="display:none;width:60px;height:60px;" id="smallImage" src="" /> 
    <img style="display:none;" id="largeImage" src="" /> 
    </body> 
</html> 

检查这个

相关问题