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

安稳与你 提交于 2019-12-24 07:59:02

问题


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);
        }
    );
}

回答1:


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



来源:https://stackoverflow.com/questions/10045065/trying-to-open-android-sd-card-using-html-javascript-and-phonegap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!