read local pdf through webview - android

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.

Related

How to Open any type of locally stored file in WebView

I have many types of file i.e. pdf, xls, xlsx, doc, docx. All these files are stored locally on the external storage, what I want to do is display the file in WebView and not open them via any external app, I have tried many different way but none of them are working for locally stored documents. The codes I have tried to open the pdf files are as follows:
My WebView setting
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setSaveFormData(true);
webView.setWebChromeClient(new WebChromeClient());
Tried codes:
1
final Uri uri = Uri.fromFile(file);
webView.loadDataWithBaseURL(uri.toString(), "", fileType, "utf-8", null);
2
webView.loadUrl(uri.toString());
3
webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + uri.toString());
Kindly help.

Loading local html file in webView 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);

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);

How to open a PDF file from server in android

I have a requirement where there is a URL = "http://www.example/Open.pdf"
Now from my android application I want to open this PDF file directly in the default PDF viewer.
The moment I click on this link on the webpage, user should be presented with a default PDF viewer opened with this document.
Note: This file should not be stored on the SD card.
How do I proceed for this implementation?
We can open PDF file in the webview without caching it. Write below code in "onCreate" method .
Working code :
String url = "http://www.example.com/abc.pdf";
final String googleDocsUrl = "http://docs.google.com/viewer?url=";
WebView mWebView=new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
mWebView.loadUrl((googleDocsUrl + url));
setContentView(mWebView);
What happens here is you open the PDF using Google Docs. Best Advantage of using above method is the lazy loading of PDF. Does not matter how heavy the PDF is. Google Docs takes care of it.
You can view the pdf in the WebView using googleDocs.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
There is no way you can open a default PDF view from your application.
If your file is on the server and you want to open it without downloading then this might also pose a greater security concern. If external applications like default adobe reader can access the content on your server, then this is breaking the security framework altogether.
So, best option would be to launch a new instance of browser or webview and show the PDF document in google docs to the user.
This way user can read the document and get back to the recent state of the application as well.
You can view the pdf in the WebView using googleDocs.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
do you have the others solution besides view pdf file using http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf

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