首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebView =网页不可用

WebView =网页不可用
EN

Stack Overflow用户
提问于 2014-06-24 20:42:03
回答 1查看 5.2K关注 0票数 0

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

网页不可用

file:///storage/sdcard0/Download/manuals/test/index4.html的网页可能暂时关闭,或者被永久移动到一个新的网址。

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

这是我的代码:

代码语言:javascript
复制
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;
}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-24 20:46:24

在您的清单xml文件中,请确保您有权限为您的应用程序读取该文件夹,并允许您使用internet (尽管您实际上不是,但它仍然是必需的)。

代码语言:javascript
复制
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24395879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档