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!
Related
I need to show a pdf file in an android app, so that the users will not be able to save or share it, nor Google will keep a copy of it.
I have thought of using iframes, but this allows the users to open the pdf file in a browser (and print or share it from there).
Here is what I came up with:
pdfView.setWebChromeClient(new WebChromeClient());
WebSettings settings = pdfView.getSettings();
settings.setPluginState(WebSettings.PluginState.ON);
pdfView.setWebViewClient(new WebViewClient());
pdfView.setBackgroundColor(0x00000000);
settings.setAllowFileAccess(false);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(false);
settings.setSaveFormData(false);
settings.setJavaScriptEnabled(true);
String html = "<iframe src=\"http://docs.google.com/gview?url=" +
path + "&embedded=true&wmode=opaque\"" +
"style=\"width:600px; height:900px;\" scrolling=\"no\"" +
"frameborder=\"0\" allowfullscreen></iframe>";
pdfView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
The solution does not have to be based on embedding and showing in a web browser.
I need to support API 16+, so using PdfRenderer is not an option.
It helped me to add sandbox="allow-same-origin allow-scripts" inside the iframe tag.
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);
I am experimenting with Android code: I would like to store one value using HTML 5 local storage. For this exercise I' using a page as simple as this one:
http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_webstorage_local_clickcount
My manifest does allow me to hit the internet, and it is min-sdk of 7.
Here is my java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabasePath("");
webSettings.setDomStorageEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://www.xyz.com/test.html");
///xyz.com/test.html is a sample :)
webview.setWebViewClient(new HelloWebViewClient());
}
My problem is that when I close the app, the locally stored value is no longer there. I can browse to the same page using the default browser, and the value is persistent even after closing the emulator, which is exactly the behavior that I am looking for.
This is probably a something extremely simple....any ideas?
It appears the empty string DatabasePath is the problem. I tried similar code and with an empty string path, the value does not persist after the app exits. If I define a specific database path, the value persists as expected.
Try:
webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
If your app use multiple webview you will still have troubles : localStorage is not correctly shared accross all webviews.
If you want to share the same data in multiple webviews the only way is to repair it with a java database and a javascript interface.
This page on github shows how to do this.
hope this help!
Couldn't get it working on all devices (especially with ICS) - even with database path, enabling DOMStorage etc. - using cookies instead helped me out.
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
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);