0

我目前有一个本地存储的 html 文件,这只是目前谷歌主页的 html。当我将 html 加载到 webview 中时,它工作正常,但似乎没有一个链接工作。例如,当我单击链接导航到 Google 图片时,它会尝试导航到该页面,但显示网页不可用。当我在浏览器中键入 url 时,我使用 toast 来显示正在打开的 url,并且 toast 显示 url(在谷歌图像示例中为http://www.google.com/imghp?hl=en&tab=wi )在我的计算机上它可以工作,但是在尝试通过手机上的 web 视图导航到它时它不起作用。有任何想法吗?下面是我的代码。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView wb = (WebView)findViewById(R.id.webView1);

    wb.getSettings().setJavaScriptEnabled(true); 
    WebSettings settings = wb.getSettings();
    settings.setJavaScriptEnabled(true); 
    settings.setDomStorageEnabled(true);

    //Listed as optimal settings for HTML5 (may need testing?).
    //Ref. http://stackoverflow.com/questions/10097233/optimal-webview-settings-for-  html5-support
    wb.setFocusable(true);
    wb.setFocusableInTouchMode(true);
    wb.getSettings().setRenderPriority(RenderPriority.HIGH);
    wb.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    wb.getSettings().setDatabaseEnabled(true);
    wb.getSettings().setAppCacheEnabled(true);
    wb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);


    WebViewClient client = new WebViewClient(){
        // you tell the webclient you want to catch when a url is about to load
        @Override
        public boolean shouldOverrideUrlLoading(WebView  view, String  url){
            Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show();
            view.loadUrl(url);
            return true;
        }
        // here you execute an action when the URL you want is about to load
        @Override
        public void onLoadResource(WebView  view, String  url){

        }

        @Override
        public void onPageFinished(WebView view, String url)
        {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
        }
    };

    wb.setWebViewClient(client);
    wb.loadUrl("file:///mnt/sdcard/Android/data/output.html");

谢谢!

4

1 回答 1

1

试试下面的代码: -

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d(TAG, "No SDCARD");
} else {
web_view.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/Android/data/output.html");
}

或者

web_view.loadUrl("file://"+Environment.getExternalStorageDirectory()+"//Android/data/output.html");

你必须检查你的android清单上的以下条目

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
于 2013-05-03T20:21:03.823 回答