【问题标题】:File in asset folder could run in webview, file in sd card could run in external browser, i want to run file in both webview and external browser资产文件夹中的文件可以在 webview 中运行,sd 卡中的文件可以在外部浏览器中运行,我想在 webview 和外部浏览器中运行文件
【发布时间】:2017-01-19 14:50:14
【问题描述】:
myWeb = (WebView) findViewById(R.id.webView1);
myWeb.getSettings().setJavaScriptEnabled(true);
myWeb.getSettings().setDomStorageEnabled(true);
myWeb.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
    activity.setProgress(progress * 1000);
        }
    });

myWeb.loadUrl("/mnt/extSdCard/THEWEB/hi.html");

//file:///android_asset/this.html 可以在 webview 中运行,但不能在 externalbrowser ///mnt/extSdCard/THEWEB/hi.html 可以运行在 外部但不在网络视图中

File file=new File("/mnt/extSdCard/THEWEB/hi.html");
Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
i.addCategory(Intent.CATEGORY_BROWSABLE);
i.setDataAndType(Uri.fromFile(file), "application/x-webarchive-xml");
            startActivity(i);

【问题讨论】:

    标签: javascript java android html css


    【解决方案1】:

    第一个选项 (/file:///android_asset/this.html) 它可以在本地浏览器中使用,因为该文件位于包文件夹中。这是私有的,其他应用程序(包括外部浏览器)不能从外部访问它。

    您写的第二个是将文件放在共享文件夹中,这可以从内部 webview 和外部 webview 访问。特别是您的代码的问题是路径:

    "/mnt/extSdCard/THEWEB/hi.html"
    

    它必须是这样的:

    "file:///mnt/extSdCard/THEWEB/hi.html"
    

    甚至更好,你可以使用:

    "file://"+Environment.getExternalStorageDirectory()+"/THEWEB/hi.html"
    

    并且您的应用应具有在清单中添加的权限以访问外部存储:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    

    【讨论】:

    • 我还有一个问题???我怎样才能让用户选择他/她想要运行文件的浏览器(“opera mini”,“google chrome”,“internet browser”),我想这样做,因为有时,其他手机没有能够运行该文件的默认浏览器,提前感谢
    猜你喜欢
    • 2011-09-30
    • 1970-01-01
    • 2017-01-02
    • 2011-06-27
    • 2016-04-29
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2012-10-19
    相关资源
    最近更新 更多