【问题标题】:File size not able to read when images is choosed from image album从相册中选择图像时无法读取文件大小
【发布时间】:2014-05-19 11:19:45
【问题描述】:

在我的 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) 相册中显示“最近”、“驱动器”、“图像”、“画廊”……当从画廊中选择图像时,只有图像大小正在变……其他比画廊图像大小无法获得..

参考了这个但没有成功

Cordova/PhoneGap Photo File Size

他们在 phonegap 文档中说:

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

Android 使用 Intent 在设备上启动相机 Activity 捕获图像,在内存不足的手机上,Cordova 活动 可能会被杀死。在这种情况下,图像可能不会出现在 Cordova 活动已恢复。

【问题讨论】:

  • 它显示什么错误?你看过 Cordova 上的 File 插件吗? cordova.apache.org/docs/en/3.0.0/cordova_file_file.md.html
  • @smj2393 没有显示...
  • 问题,你是在真机上测试还是在模拟器上测试?
  • @Gajotres 在我的 nexus 设备 v 4.4.2 中
  • 很好,现在告诉我你第一次打开菜单(这将允许你选择一张图片)时,你的手机会显示哪些图片库?相信我,这是一个重要的问题

标签: javascript android jquery-mobile cordova


【解决方案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>

检查一下

【讨论】:

    猜你喜欢
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2019-04-07
    • 2019-03-20
    • 2022-08-19
    • 2017-05-21
    相关资源
    最近更新 更多