2014-06-24 65 views
0

我試圖使用webView來顯示設備上的HTML文件,而不是互聯網。我在/下載文件夾中有我的html文件。當我啓動應用程序,我得到以下錯誤:WebView =網頁不可用

網頁無法

該網頁的文件:///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; 
} 

} 
+0

可能重複(http://stackoverflow.com/questions/6160165/web-page-not-available-error) –

回答

0

在你的清單XML文件,請確保您有權限讀取該文件夾爲您的應用程序和使用互聯網(即使你實際上是不許可的,它仍然是需要)。 [找不到網頁錯誤]的

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

謝謝:)這是現在的工作。 – Kyle