Trying to open Android SD card using HTML, Javascript and Phonegap

I am trying to open the SD card and upload a file in Android using Phonegap. Below is the code where I am appending the SD card content to my HTML, but nothing is displaying. I am using cordova.js, jquery1.7.1.js. Below is my code:

My Javascript:

function onDeviceReady() {
    getFileSystem();
}

function getFileSystem() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
        function(fileSystem) { // success get file system
            root = fileSystem.root;
            listDir(root);
        }, 
        function(evt) { // error get file system
            console.log("File System Error: "+evt.target.error.code);
        }
    );
}

function listDir(directoryEntry) {
    if (!directoryEntry.isDirectory) 
        console.log('listDir incorrect type');

    $.mobile.showPageLoadingMsg(); // show loading message

    currentDir = directoryEntry; // set current directory
    directoryEntry.getParent(
        function(par) { // success get parent
            parentDir = par; // set parent directory
            if ((parentDir.name == 'sdcard' && currentDir.name != 'sdcard') || parentDir.name != 'sdcard') 
                $('#backBtn').show();
        }, 
        function(error) { // error get parent
            console.log('Get parent error: ' + error.code);
        }
    );

    var directoryReader = directoryEntry.createReader();
    directoryReader.readEntries(
        function(entries) {
            var dirContent = $('#dirContent');
            dirContent.empty();

            var dirArr = new Array();
            var fileArr = new Array();
            for (var i=0; i < entries.length; ++i) { // sort entries
                var entry = entries[i];
                if (entry.isDirectory && entry.name[0] != '.') 
                    dirArr.push(entry);
                else if (entry.isFile && entry.name[0] != '.') 
                    fileArr.push(entry);
            }

            var sortedArr = dirArr.concat(fileArr); // sorted entries
            var uiBlock = ['a','b','c','d'];

            for (var i=0; i < sortedArr.length; ++i) { // show directories
                var entry = sortedArr[i];
                var blockLetter = uiBlock[i%4];
                //console.log(entry.name);
                if (entry.isDirectory)
                    dirContent.append('<div class="ui-block-' + blockLetter + '"><div class="folder"><p>' + entry.name + '</p></div></div>');
                else if (entry.isFile)
                    dirContent.append('<div class="ui-block-' + blockLetter + '"><div class="file"><p>' + entry.name + '</p></div></div>');
            }
            $.mobile.hidePageLoadingMsg(); // hide loading message
        }, 
        function(error) {
            console.log('listDir readEntries error: ' + error.code);
        }
    );
}

Answers:

Answer

Have you taken a look in the Cordova API Documents? I Tried this sample code and it worked just fine. Link to API and the sample code

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.