当从图像相册中选择图像时,无法读取文件大小

File size not able to read when images is choosed from image album

本文关键字:图像 读取 文件大小 选择      更新时间:2023-09-26

在我的phonegap应用程序(android版本4.4.2)我需要从sd卡中选择图像。在这种情况下,我无法读取图像大小和名称。我的代码是。

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)相册显示像"最近","驱动器","图像","画廊",…当从图库中选择图像时,只有图像大小可以获取..而图库图像大小无法获取..

引用了这个,但是没有得到成功

Cordova/PhoneGap照片文件大小

在phonegap文档中他们说:

仅限Android 4.4: Android 4.4引入了新的存储访问框架,使用户更容易浏览和打开文档跨所有首选文档存储提供程序。科尔多瓦有尚未与这个新的存储访问框架完全集成。因此,getPicture()方法将不能正确返回当用户从"最近"、"驱动器"、"图片"中选择图片时,或"外部存储"文件夹,当destinationType为FILE_URI时。然而,用户将能够正确地选择任何图片,如果他们会先通过"图库"应用程序。潜在的解决方法这个问题记录在这个StackOverflow问题上。请参阅CB-5398来跟踪这个问题。

Android使用intent来启动设备上的摄像头活动捕捉图像,在内存较低的手机上,Cordova活动可能会被杀。在此场景中,图像可能不会出现Cordova活动恢复。

<!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>

检查这个