WebView =网页不可用

时间:2014-06-24 20:42:04

标签: java android eclipse webview android-webview

我正在尝试使用webView来显示设备上的HTML文件,而不是互联网。我在/ Download文件夹中有我的html文件。当我启动应用程序时,我收到以下错误:

网页不可用

file:///storage/sdcard0/Download/manuals/test/index4.html上的网页可能暂时关闭,或者可能已永久移至新的网址。

我知道文件在那里但它不会显示它。

这是我的代码:

package com.asstechmanuals.techmanual;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


  mWebView =  (WebView) findViewById(R.id.webview);
  mWebView.getSettings().setJavaScriptEnabled(true);




File fileStandard = new File("/storage/sdcard0/Download/manuals/test/index4.html");
File fileNewStandard = new        File("/storage/sdcard0/Download/manuals/test/index4.html");
File fileKitKat = new File("/storage/sdcard0/Download/manuals/test/index4.html");


  if(fileStandard.exists())      
      mWebView.loadUrl("file:///storage/sdcard0/Download/manuals/test/index4.html");
  else if(fileNewStandard.exists()) 
      mWebView.loadUrl("file:///storage/sdcard0/Download/manuals/test/index4.html");
  else if(fileKitKat.exists()) 
      mWebView.loadUrl("file:///storage/sdcard0/Download/manuals/test/index4.html");
  else
      mWebView.loadUrl("file:///storage/sdcard0/Download/manuals/test/index4.html");

  mWebView.setWebViewClient(new vwClient());

}


private class vwClient extends WebViewClient{

   @Override
   public boolean shouldOverrideUrlLoading(WebView webview, String url)
   {
        webview.loadUrl(url);

        if (url.toLowerCase().contains(".pdf"))
        {

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "application/pdf");
            startActivity(intent);

        }


        return true;
   }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
   if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
   {
       mWebView.goBack();
       return true;
   }
   return super.onKeyDown(keyCode, event);
}
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

在您的清单xml文件中,请确保您有权阅读该应用的该文件夹以及使用互联网的权限(即使您实际上并非如此,仍然需要)。

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