【问题标题】:Display all images from Sdcard显示 SD 卡中的所有图像
【发布时间】:2013-06-21 09:36:10
【问题描述】:

我正在使用 Phonegap 2.2.0。 我想从我的 android 设备中获取所有图像/照片/壁纸以及图像保存路径。 到目前为止我所做的是

    <!DOCTYPE html>
<html>
  <head>

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

    document.addEventListener("deviceready",onDeviceReady,false);

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

     function onFail(message) {
      alert('Failed because: ' + message);
    }

    function gotFS(fileSystem) {
        fileSystem.root.getFile("1.jpg", {create: true}, gotFileEntry, fail);
    }

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

    function gotFile(file){
        readDataUrl(file);  
    }

    function readDataUrl(file) {
           var reader = new FileReader();
           reader.onloadend = function(evt) {
           console.log("Read as data URL");
           console.log(evt.target.result);
           document.getElementById("smallImage").style.display='block'; 
           document.getElementById("smallImage").src = evt.target.result;   
        }; 
        reader.readAsDataURL(file);
    }

    function fail(evt) {
        console.log(evt.target.error.code);
    }
    </script>
  </head>
  <body>
    <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
    <img style="display:none;" id="largeImage" src="" />
  </body>
</html>

代码只给我一个图像,但我想要所有图像。 任何建议都会很棒。

谢谢。

【问题讨论】:

    标签: javascript android file jquery-mobile cordova


    【解决方案1】:

    您需要从根 FS 创建一个 DirectoryReader 并遍历所有条目以查找 .svg 文件。

    function gotFS(fileSystem) {
        var reader = fileSystem.root.createReader();
        reader.readEntries(gotList, fail)l
    }
    
    function gotList(entries) {
        var i;
        for (i=0; i<entries.length; i++) {
            if (entries[i].name.indexOf(".svg") != -1) {
                uploadPhoto(entries[i].fullPath);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-07
      • 2011-06-20
      • 1970-01-01
      • 2023-03-27
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多