Loading local html file in webView android - android

I have to load an existing html file into a WebView that is located at this path in the file system:
/data/data/com.example.example/files/file.html
But, when the WebView loads it, I don't see anything.
Who can help me?
WebView code (assuming path is the path I've written above):
WebView webView = (WebView)findViewById(R.id.webView1);
File htmlFile = new File(path);
if(htmlFile.exists())
{
webView.loadUrl(htmlFile.getAbsolutePath());
}

Try this, adding in a file:/// and doing it a little differently:
WebView webView = (WebView)findViewById(R.id.webView1);
webview.loadUrl("file:///data/data/com.example.example/files/file.html");
Instead of this, however, you could just put the file into your assets folder in the source code, and then do this:
WebView webView = (WebView)findViewById(R.id.webView1);
webview.loadUrl("file:///android_asset/file.html");

The html file should be placed in the assets folder, which will belong in the root directory of your project.
So move your file to in case of eclipse
assets/index.html
In an Android Studio project use this folder:
/app/src/main/assets/index.html
Now use
WebView wv= (WebView)findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/index.html");

You need to implement a ContentProvider to map local files to uris as explained in this link how to display a local file into Android Webview
or you just load any html page from Assets folder like below:
WebView wv= (WebView)findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/yourfile.html");
wv.getSettings().setJavaScriptEnabled(true);

Try this:
web = (Webview) findViewById(R.id.webview);
web.setWebClient(new WebViewClient());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setAllowFileAccess(true);
web.getSettings().setAllowFileAccessFromFileURLs(true);

Related

how to load pdf file in a webview from assets folder

I ahve a webview in my layout screen. I need to display a PDF file in that webview from my assets folder. I tried below code. While running app displays zoom in and out controls with a blank page(our PDF file is kind of large.is it due to that?).
webView = (WebView) findViewById(R.id.webView1);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) //required for running javascript on android 4.1 or later
{
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
}
settings.setBuiltInZoomControls(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/test.pdf");
I dont want to use external apps to read my PDF. is it possible?then how?
You can use pdf.js from Mozilla to display a PDF in a webview inside an Android application.
Here is the solution...
https://stackoverflow.com/a/21383356/2260073
This question is answered already . For the details refer the following links.
How to read pdf in my android application?.
One more is
stackoverflow.com/questions/10299839/how-to-read-pdf-in-my-android-application/10352422#10352422
After searching a lot about this i came up with my own kind of solution
I used this website to convert my PDF to DOCX and then DOCX to HTML using online converters and then used the following code to load the HTML in my Webview.
String htmlString = "<p>YOUR HTML CODE</p>";
webView.loadData(htmlString, "text/html", "UTF-8");
Hope this helps!

Display HTML page with external JavaScript via Android assets

So I have been looking everywhere and doesn't seems to find working answer for this. I have dynamic html page create using iMapBuilder and it has 3 external js files. Before I have added dynamic contents html was working just fine being under assets directory with following command:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ground_floor);
WebView wv;
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("file:///android_asset/imap5custom.html");
}
}
After adding dynamic content with 3 Java Script external files which are also under root assets folder, html doesn't work any more. I have tried:
mWebView.loadDataWithBaseURL
but still didn't work for me.

android load local(in Asset/Res/External Storage) pdf file in webview

I want to display pdf file in webview.
I try using this code it displays blank page.
File retrieve perfacly but not display in webView.
Put screen shoot of webview displays webview when i run the application.
display blank webview. like this....
private WebView view;
Uri path;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (WebView) findViewById(R.id.webView);
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Documents/MyPDFFILE.pdf");
path = Uri.fromFile(file);
view.setContentDescription("application/pdf");
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setLoadsImagesAutomatically(true);
view.getSettings().setPluginsEnabled(true);
view.getSettings().setAppCacheEnabled(true);
view.loadUrl(path.toString());
// view.loadUrl("http://grail.cba.csuohio.edu/~matos/notes/cis-493/lecture-notes/Android-Chapter10-WebKit.pdf");
}
Because, Android WebView doesn't support PDF file functionality. And WebView cannot render PDFs.
The URL you posted on your comments it displayed pdf file in native web browser because of it uses Google Docs.
If you want to display pdf file in your webview then using Google Docs pass url of your pdf file which located on server not in your local.
Update:
Code for display pdf file located on net in webview.
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "http://www.xxxx.com/xxxx/xxxx.pdf";
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);

read local pdf through webview

I am doing one application to read pdf from sdcard through webview. I used the following code.
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new HelloWebViewClient());
File file = new File(Environment.getExternalStorageDirectory()
+ "/MotoronAug.pdf");
Uri uri = Uri.fromFile(file);
webview.loadUrl(uri.toString());
but i got a blank page. But when i give a web address
webview.loadUrl("http://docs.google.com/viewer?url=http%3A%2F%2Fresearch.google.com%2Farchive%2Fbigtable-osdi06.pdf");
I can read it from webview what is the problem in my code?...I have MotoronAug.pdf file in my sdcard.Help me friends
Online or local both wont be rendered by webview.

Android : How to Load PDF?

Is there any way to load PDF file from Asset/URL in Browser or WebView or any other way?
There is no native way to do this at the moment. However, you could write your own pdf viewer activity. Also, check out RepliGo Reader and PDF GView.
Instead loading PDF file from local resource, you can think about open it online using Google Docs Viewer:
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);

Categories

Resources