如何使用javascript在android phonegap中获取sdcard目录的完整路径?

时间:2013-08-22 07:24:11

标签: javascript android cordova

我正在使用PhoneGap 2.8.0 for Android app。

需要在phonegap中使用javascript获取SD卡的完整路径,因为在我的手机中它将位置显示为file:///sdcard/external_sdcard/,但在我的朋友手机中显示file:///mnt/external_sdcard/ ...

我的文件夹名称是sdcard中的App_files

3 个答案:

答案 0 :(得分:2)

请尝试此代码..

通过window.appRootDir.fullPath获得完整路径但在此之前您必须使用dirReady();

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/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 complete: " + entry.fullPath);
                }, function(error) {
                    alert("download error" + error.source);
                });
            }
        </script>
    </head>

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

</html>

答案 1 :(得分:2)

您也可以通过此代码获取您的目录。 来自phonegap文档的代码参考。

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

答案 2 :(得分:1)

最后我发现我们无法获得外部SD卡路径,

Environment.getExternalStorageState();

仅返回内部SD卡路径

参考: http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()