I am aware that this question has been asked many times, but I still cannot get it to work. I have a pdf in the raw folder and I am trying to open it in a webview using "https://drive.google.com/viewerng/viewer?embedded=true&url=" to open it in the webview.
I think my problem is I can't get the correct path for the pdf file. I've tried it three different ways, and either the webview says I get an error (the url doesn't exist), or it "opens" the pdf, but the webview displays this text in the middle, "No preview available".
These are the three ways I've tried to get the path of the file.
"android.resource://com.an.example/raw/filename"
"android.resource://" + getPackageName() + "/" + R.raw.filename;
Environment.getExternalStorageDirectory() + "/filename.pdf"
For methods 1 and 3 I have tried "filename" and "filename.pdf"
None of these are allowing the pdf to load in the webview. Below is the code I use to load the pdf. (Note: this method does work if I use a web url and not a local file)
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(true);
String pdf = "one_of_the_above_methods";
webview.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
What am I doing wrong? Is there a better way to load a local pdf inside my app without having it take up the entire activity page?
I have a pdf in the raw folder and I am trying to open it in a webview using "https://drive.google.com/viewerng/viewer?embedded=true&url=" to open it in the webview.
That has never been possible.
Is there a better way to load a local pdf inside my app without having it take up the entire activity page?
The best thing is to not load it inside your app at all. Use the user's preferred PDF viewing app, via ACTION_VIEW.
Beyond that, you could use a slightly modified version of PDF.js on Android 4.4+. Or, use AndroidPdfViewer on a wider range of Android versions, though it makes your APK a lot bigger. See this blog post for more context on these and other (worse) options.
Also, here are sample apps demonstrating:
PDF.js: https://github.com/commonsguy/cw-omnibus/tree/v8.10/PDF/PdfJS
AndroidPdfViewer: https://github.com/commonsguy/cw-omnibus/tree/v8.10/PDF/Pdfium
Related
I'm using Android Studio's Webview to convert my web app to mobile app.
my webapp has a login which shows your account's files (pdf files).
but running my default app on Android Studio, won't open these pdf files.
I read here to use Google viewer and just put the link of your PDF file just like below,
WebView webview=new WebView(this);
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);
however, the webapp is setup wherein if you open the PDF file, it doesn't have the .pdf extension. Example is: https://mysite/files/myfiles/1234567_00/. As you see, the link does not point to a direct pdf file (.pdf extension), instead it only had "/1234567_00/". When I try to replace the link above with the URL (without .pdf extension), it won't open.
Is there a way I can get this to work? What should I change here?
I am using web-view in my native android application. But the issue I am facing is that I have a PHP file generatePDF.php which takes sessions, parameters from URL and output pdf file on that URL. The issue is that when I pass that URL to webview directly it doesn't load pdf in webview but instead if I pass direct pdf file link through anchor link it loads.
WebView wv = findViewById(R.id.wvBrowser);
WebSettings ws = wv.getSettings();
wv.loadUrl("http://localhost/generatePDF.php?uid=1");
Above code don't load pdf. But if i add some anchor tag and add above path in href then it opens that pdf.
wv.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=http://localhost/generatePDF.php?uid=1");
I tried like above and it loads pdf directly but it doesn't pass the sessions.
Kindly guide how can I load my generatePDF file directly in web-view
WebView has no ability to display PDFs. Either:
Use something like PDF.js to be able to parse and render PDF in a WebView, or
Use a library to render a PDF as its own view
See this blog post for more on this subject.
I have already created the app, but it needs to be connected to the internet to work. In essence it just displays the website through an app. I would like to have the website installed on the app so that it can load without requiring an internet connection.
I have downloaded my webpage while, and added it to the Android SDK. Its saved it under the Java section. Now the code I used to load the webpage while online was the following:
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://kuglerdesign.com/");
mWebView.setWebViewClient(new MyAppWebViewClient());
I have searched on stackoverflow for a similar question, and have found the following lines of code:
URL url = this.getClass().getResource("/com/path/to/file.txt")
File file = new File(url.toURI());
and these from a different answer:
File currFile = new File(getClass().getClassLoader().getResource("the_file.txt").getFile());
My question is how to implement these to a file thats in the Android SDK. Do I just replace all the mWebView and WebSettings or do I still need them as its a HTML file?
Or would I be better off just to set the mWebView Url to a local file?
So replacing the following line of code:
mWebView.loadUrl("http://kuglerdesign.com/");
with this line :
mWebView.loadUrl("file:///kuglerdesign.html");
Any help would be greatly appreciated.
(sorry for being such a novice at Java :( ...)
You have to put your html-files into the assets directory.
d.android.com states for the assets folder:
You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
The WebView can directly load your files, by using file:///android_asset/ as URI path, which can be read here.
That would make your call as easy as mWebView.loadUrl("file:///android_asset/kuglerdesign.html");
I have looked at 50+ sources, none give the exact information
loading html into webview with images in the code does make webview load the images, only when it exists in the assets
I can make webview show images from other sites as well
I am creating images dynamically, and I want to show them in the webview
I cannot copy files to the assets folder, so I cannot load them from there either
It is as if it is carefully planned to prevent a very specific functionality that I am striving for.
There are many answers providing solutions, none do work, and they are from many years ago.
I want to load files that exist in the getFilesDir(); directory, because they are created there.
I'd appreciate help, thank you.
I have tried
all these:
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
& many many combinations of URLS trying to access the file at getFilesDir(); in the HTML code none work except for loading from other sites, and from assets which I cannot write to.
I can add images directly into HTML by calculating the base64 string of a image and adding the HTML tag+code/url:
<img src="data:image/jpg;base64,######base64string#######">
how you generate the base 64 string is up to you. I am still searching for a method to make webview load images from the getFilesDir();, the working directory of my app.
I have a WebView on my Android application which loads (WebView.loadUrl()) different local HTML files from phone's internal storage. I would like to include some custom css styles for them.
Now, I could have my app edit every HTML file and add linking reference for the CSS file.
I could also read the file contents, add the CSS linking and use WebView.loadData() to load it.
But is it possible to do this a lot simpler and efficiently?
Note: The HTML files are downloaded from a website. So editing them manually is not possible in this case, but once downloaded they can be edited via the app if necessary.
One possibility (I have not tried this):
WebView.loadDataWithBaseURL(String baseUrl, String data, ..)
takes a baseURL for the document to use to resolve relative URLs. Take a look at the CSS url and construct baseURL so that CSS url will reference local CSS file.