Skip to content Skip to sidebar Skip to footer

Unable To Read Image Files From Sdcard In Phonegap: Android

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 instead a q

Solution 1:

Here is a working example :

EDITED:

<!DOCTYPE html><html><head><title>Pick Photo</title><scripttype="text/javascript"charset="utf-8"src="cordova-2.2.0.js"></script><scripttype="text/javascript"charset="utf-8">document.addEventListener("deviceready",onDeviceReady,false);

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

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

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

    functiongotFileEntry(fileEntry) {
        fileEntry.file(gotFile, fail);
    }

    functiongotFile(file){
        readDataUrl(file);  
    }

    functionreadDataUrl(file) {
           var reader = newFileReader();
           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);
    }

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

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

Hope this helps you.

Thanks.

Solution 2:

try this:

<script>document.addEventListener("deviceready",onDeviceReady,false);

functiononDeviceReady(){ 
  //This file have to existvar location= "file:///storage/emulated/0/DCIM/Camera/20141105_124208.jpg";
  window.resolveLocalFileSystemURL(location, function(oFile) {
    oFile.file(function(readyFile) {
      var reader= newFileReader();
      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>

Post a Comment for "Unable To Read Image Files From Sdcard In Phonegap: Android"