最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to get the full path of sdcard directory in android phonegap with javascript? - Stack Overflow

programmeradmin1浏览0评论

I am using PhoneGap 2.8.0 for android app.

Need to get the full path of sdcard using javascript in phonegap, because in my mobile it shows the location as file:///sdcard/external_sdcard/ but in my friend mobile its shows file:///mnt/external_sdcard/...

my folder name is App_files in sdcard

I am using PhoneGap 2.8.0 for android app.

Need to get the full path of sdcard using javascript in phonegap, because in my mobile it shows the location as file:///sdcard/external_sdcard/ but in my friend mobile its shows file:///mnt/external_sdcard/...

my folder name is App_files in sdcard

Share Improve this question edited May 5, 2014 at 20:24 manlio 19k14 gold badges82 silver badges134 bronze badges asked Aug 22, 2013 at 7:24 DhamuDhamu 1,7525 gold badges22 silver badges47 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

you can also get your directory by this code. code reference from phonegap documentation.

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

    // device APIs are available
    //
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
    }

    function onFileSystemSuccess(fileSystem) {
        fileSystem.root.getDirectory("App_files", {create: false, exclusive: false}, onGetDirectoryWin, onGetDirectoryFail);
    }

    function fail(evt) {
        console.log(evt.target.error.code);
    }

    var onGetDirectoryWin = function(parent) {

    }
    var onGetDirectoryFail = function() {
        console.log("error getting dir")
    }

Please try this code..

There is window.appRootDir.fullPath through you get full path but before that you must use dirReady();

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3/TR/html4/loose.dtd">
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>
            Insert title here
        </title>
        <script type="text/javascript" src="lib/android/cordova-1.7.0.js">
        </script>
        <script type="text/javascript">
            window.appRootDirName = "download_test";
            document.addEventListener("deviceready", onDeviceReady, false);

            function onDeviceReady() {
                console.log("device is ready");
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
            }

            function fail() {
                console.log("failed to get filesystem");
            }

            function gotFS(fileSystem) {
                console.log("filesystem got");
                window.fileSystem = fileSystem;
                fileSystem.root.getDirectory(window.appRootDirName, {
                    create: true,
                    exclusive: false
                }, dirReady, fail);
            }

            function dirReady(entry) {
                window.appRootDir = entry;
                console.log("application dir is ready");
            }


            downloadFile = function() {
                var fileTransfer = new FileTransfer();

                var url = "http://www.irs.gov/pub/irs-pdf/fw4.pdf";
                var filePath = window.appRootDir.fullPath + "/test.pdf";
                fileTransfer.download(
                url, filePath, function(entry) {
                    alert("download plete: " + entry.fullPath);
                }, function(error) {
                    alert("download error" + error.source);
                });
            }
        </script>
    </head>

    <body>
        <a href="#" onclick="downloadFile()">Download File</a>
    </body>

</html>

Finally i found we can't get the external SD Card Path,

Environment.getExternalStorageState();

Its returns only Internal SD Card path

Reference: http://developer.android./reference/android/os/Environment.html#getExternalStorageDirectory()

发布评论

评论列表(0)

  1. 暂无评论