android webview loading images - java

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.

Related

How to open local pdf file in WebView Android Studio

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

Trying to load an app from a local file rather than from the website

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

Java prog to capture images from a webpage and output their links to an Excel file

I want a Java app that would capture all the images (and preferably data in other tags too) from a webpage and write their links to an excel file.
While I know my way around Excel files and Java, I was just wondering if there's any way to capture images from web pages.
A quick google search didnt help
Obviously there is.
Since images are in the source code, you can start from the simpliest solution - getting the page source, retrieve image links and download them.
KISS ;-)
Probably you need to parse the html of the webpage and get the links referring to images from respective html tags.

save webpages for offline browsing

i am trying to create an android application that saves webpages to use it in offline-browsing, i was able to save the webpage but the problem was in the contents (images, javascripts,..etc), is there a way to do so programmatically, i use eclipse and test my work on an emulator.
hm, I am afraid you should parse html's yourself (I mean do that with a properly lib) and store all resources (css, js, images, videos etc.) too.
s. how it is done in a java crawler: open source crawlers
You will need to search for all images, javascript files, css files, etc... and download them, saving them to the same relative path to the HMTL files - Assuming the html is coded with relative paths (images/image.png) and not absolute paths (http://www.domain.com/image/image.png).
You can pretty easily search the html string for <img, <script, <link etc.. and parse from there - or you can find a 3rd party html parser

Load HTML file to WebView with custom CSS

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.

Categories

Resources