如何在Android中的WebView中使用Javascript LocalFileSystem访问SDCARD?

时间:2013-02-15 10:28:19

标签: javascript android html5 webview sd-card

我想在Android浏览器的webview中显示目录列表,只需单击webview中的按钮。

webView已推出正常,它正在侦听事件,但我收到了异常

Uncaught ReferenceError: LocalFileSystem is not defined at file:///android_asset/FirstFile.html:11

这是完整的代码

的活动:

public class Home extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WebView webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebChromeClient(new WebChromeClient());

    webview.loadUrl("file:///android_asset/FirstFile.html");


    setContentView(webview);

}

}

这是FirstFile.xml

<!DOCTYPE html>
<html>
<body>
    <script type="text/javascript" charset="utf-8">
        function getFileSystem() {
            window.requestFileSystem = window.requestFileSystem
                    || 

            window.requestFileSystem(LocalFileSystem.PERSISTENT, 1024 * 1024,
                    onFileSystemSuccessUpload, fail);

            window.resolveLocalFileSystemURL("file:///mnt/sdcard",
                    onResolveSuccess, fail);

            document.getElementById("text1").innerHTML = "Hii its done";

        }

        function onResolveSuccess(fileEntry) {
            document.write("URI Resolved");
        }

        function fail() {
            console.log("Error getting system")
        }
function successReader(entries) {
            var i;
            for (i = 0; i < entries.length; i++) {
                alert(entries[i].name);
                if (entries[i].isDirectory == true) {
                    var directoryReaderIn = entries[i].createReader();
                    directoryReaderIn.readEntries(successReader, fail);

                }

            }
        }
    </script>

    <input type="text" id="text1">
    <button onclick="getFileSystem()">Click Me !!</button>
</body>
</html>

请给我一个合适的解决方案

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。我们需要从libs导入cordova.jar并将cordova.js包含在我们的HTML页面中。

现在我可以轻松访问SD卡的所有目录。