【问题标题】:WebView = Web page not availableWebView = 网页不可用
【发布时间】:2014-06-24 20:42:03
【问题描述】:

我正在尝试使用 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;
}

}

【问题讨论】:

标签: java android eclipse webview android-webview


【解决方案1】:

在您的清单 xml 文件中,确保您有权为您的应用读取该文件夹并有权使用互联网(即使您实际上没有,但仍然是必需的)。

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

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多