Unable to read image files from sdcard in phonegap: android

前端 未结 2 1186
醉酒成梦
醉酒成梦 2020-12-31 18:36

I am a phonegap newbie. I am trying to read and image file from sdcard in android using phonegap official tutorial. The problem is that the image is not being displayed inst

相关标签:
2条回答
  • 2020-12-31 19:22

    Here is a working example :

    EDITED:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Pick Photo</title>
    
        <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>
    

    This will allow you to pick any specified image from sdcard.

    Hope this helps you.

    Thanks.

    0 讨论(0)
  • 2020-12-31 19:41

    try this:

    <script>
    
    document.addEventListener("deviceready",onDeviceReady,false);
    
    function onDeviceReady(){ 
      //This file have to exist
      var location= "file:///storage/emulated/0/DCIM/Camera/20141105_124208.jpg";
      window.resolveLocalFileSystemURL(location, function(oFile) {
        oFile.file(function(readyFile) {
          var reader= new FileReader();
          reader.onloadend= function(evt) {
            document.getElementById("smallImage").style.display='block'; 
            document.getElementById("smallImage").src = evt.target.result;
          };
          reader.readAsDataURL(readyFile); 
        });
      }, function(err){
        console.log('### ERR: filesystem.directoryUp() - ' + (JSON.stringify(err)));
    });
    
    </script>
    
    0 讨论(0)
提交回复
热议问题