Issue
I am trying to use webView to display an HTML file that is on the device and not the internet. I have my html files in the /Download folder. When I launch the application I get the following error:
Webpage not available
The webpage at file:///storage/sdcard0/Download/manuals/test/index4.html might be temporarily down or it may have been moved permanently to a new web address.
I know the file is there but it will not display it.
Here is my code:
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;
}
}
Solution
In your manifest xml file, make sure you have permission to read that folder for your app and permission to use internet (even though you actually are not, it is still required).
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
Answered By - TronicZomB
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.