In my app I want the user to read the pdf files inside my app without any option to download them. How can I do this? I am using the following code but not working-
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.loadUrl("https://docs.google.com/viewer?"+pdf_url);
Is there any other way to do this? If you can do it?
You can open pdf from URL online in Android without a download option.
Kotlin
val path="https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf"
activityWebViewBinding.webview.settings.loadWithOverviewMode = true
activityWebViewBinding.webview.settings.javaScriptEnabled = true
val url = "https://docs.google.com/gview?embedded=true&url=$path"
activityWebViewBinding.webview.loadUrl(url)
Java
String path="https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf";
webview.settings.setLoadWithOverviewMode(true);
webview.settings.setJavaScriptEnabled(true);
String url = "https://docs.google.com/gview?embedded=true&url="+path;
webview.loadUrl(url);
You can simply launch intent as below
val pdfUrl = "ENTER_PDF_URL_HERE"
val intent: Intent = Intent(Intent.ACTION_VIEW, Uri.parse(pdfUrl))
.addCategory(Intent.CATEGORY_BROWSABLE)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
This would simply open it up device's pdf viewer app which comes in almost all android devices com.google.android.apps.docs
You can also safeguard this code by querying packages beforehand and adding fallbacks
Related
I have created a web app in Android studio using WebView. I have loaded a WordPress site in android WebView. The WordPress site exports a pdf report on a button click When i load it on chrome browser.
The website loaded in chrome
When i click on PDF button shows the window
and
and finally it starts downloading the pdf file
and the generated pdf report looks like:
But the problem occurs when i use the Android App created in WebView. When i click on PDF button it does nothing and i get a Debug log
D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
[InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
I have manifest permission set to
uses-permission android:name="android.permission.INTERNET"
Code here
private static final String WEB_URL = "http://workmanager.midzonetechnologies.com/";
webView = findViewById(R.id.webView);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.getSettings().setBlockNetworkLoads(false);
webView.getSettings().setAppCacheMaxSize( 10 * 1024 * 1024 ); // 10MB
webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(WEB_URL);
Please tell is the problem related to manifest file permission or run time permission, Please give a solution.
The Android app that i created in WebView.
What you are seeing happening in Chrome is:
noticing the downloadable file based on path: mywebsite.com/randomPath/randomFile**.pdf**
request download permissions
download the file to it's local storage
open the PDF file using Google Drive
If you want that behaviour you have to code all those steps in your app.
A fast solution to this is to intercept the url, check if it is a pdf file and then use a browser to open it.
webView.webviewClient = object : WebViewClient() {
#TargetApi(Build.VERSION_CODES.N)
fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest): Boolean {
val url = request.url.toString()
if (url.takeLast(4) == ".pdf") {
startActivity(
Intent(
Intent.ACTION_VIEW,
"https://docs.google.com/gview?embedded=true&url=${request.url}"
)
)
return true
}
return false
}
}
You would probably have to have different steps in here. As #Florin T. stated, you would first have to get the Permission required for your App:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In the next step you would be detect wether the system can be downloading anything.
This coud be done like so:
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}});
Source here
If you then click on the PDF-Button in your WebView, the system should be handling the download on your phone. This means that you can then be opening your download as if it would have been downloaded via chrome.
In the last step, you would have to get the saved loaction from the download and start an Intent to open it with a local PDF-App. This could be done via android intents.
I'm trying to load a pdf in a WebView with google docs, but the width of the psd is cut in half. I have tried different options but none of them looks great.
This code show what i have already tried:
webView.Settings.JavaScriptEnabled = true;
webView.Settings.SetLayoutAlgorithm (WebSettings.LayoutAlgorithm.Normal);
//webView.Settings.SetLayoutAlgorithm (WebSettings.LayoutAlgorithm.SingleColumn);
//webView.Settings.LoadWithOverviewMode = true;
//webView.Settings.UseWideViewPort = true;
webView.SetWebViewClient(new WebViewClient());
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webView.LoadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
Opening a new intent and downloading the pdf is not an option by the way.
Also, i'm using the Xamarin framework.
I'm also seeing this alot in my application output:
[skia] xxxxxxxxxxxxxxxxxx clip not implemented
try this
wv.getSettings().setJavaScriptEnabled(true);
String pdf = "http://ga.berkeley.edu/wp-content/uploads/2015/02/pdf-sample.pdf";
String urlmain = "https://docs.google.com/viewer?url=" + pdf;
wv.loadUrl(urlmain);
and make your webView height match_parent, it will surely work
I have a webview with javascript enabled and I'm attempting to display pdf, docs, etc... using google docs view url. The problem is that the webview is rendering html instead of the doc. I should mention that this also occurs with a url to a pdf or doc from my dropbox account.
string pdf = "https://docs.google.com/document/d/1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing";
webViewcontent.LoadUrl("https://docs.google.com/gview?embedded=true&url=" + pdf);
This is what is showing:
I think the url you are looking for is...
string url = "https://docs.google.com/document/d/1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing";
string myUrl = string.Format("https://docs.google.com/viewer?url={0}", url);
webViewcontent.LoadUrl(myUrl);
This will work without the user having a google account.
However, since your url is using https it may require authentication.
string pdf = "https://docs.google.com/document/d/1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing";
webViewcontent.LoadUrl("https://docs.google.com/gview?embedded=true&url=" + pdf);
the final url of this will be https://docs.google.com/gview?embedded=true&url=**https://docs.google.com/document/d/**1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing, I think you want to remove everything except for the document id.
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);