I want to open files into WebView. My all files saved in internal storage. I want to keep my files private. Files type can be PDF, txt and MS-Word etc.
I have tried using Content Provider but it not works for PDF and MS-Word files.
I tried the below code for open in WebView.
String doc= "<iframe src='http://docs.google.com/viewer?url="+Uri.parse(path)+"width='100%' height='100%' style='border: none;'></iframe>";
web.loadUrl(doc);
How can I open internally stored files in webview?
I have tried using Content Provider but it not works for PDF and MS-Word files.
A ContentProvider works equally well for all MIME types.
I tried the below code for open in WebView.
That is using an undocumented, unsupported URL for displaying Web-hosted content in a Google-supplied document-rendering page. That has no ability to access files on a device.
How can I open internally stored files in webview?
For certain types of files — such as plain text, HTML, and some image formats — WebView will be able to display the content directly. Just use loadUrl() with the file URL.
For a few other types of files, you may be able to find third-party JavaScript that you can use. For example, for PDF, you could modify Mozilla's PDF.js to be able to work with local PDFs.
The only general-purpose solution is for you to learn JavaScript and write your own document-rendering code. Put that JavaScript (and related HTML/images/CSS) in your Android app, and have it open the local filesystem content using file URL values.
Related
I want to write a number of html files on my PC. I then want these files bundled into my app installable, so when the app is installed on the smart phone, it already has these files stored locally.
I then want to use a button in my existing app to select 1 of these files and then to have this file opened up for viewing in a separate web browser (outside of my app).
I have tried using code like this below and it works fine for running html pages from external sites:
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))
startActivity(browserIntent)
However if I try and publish from a file on my Android like Uri.parse("file://android_assets/test.htm") then my app crashes.
Can anyone suggest how I can publish html files from my Android app in a new browser, I would prefer to use something like Intent ACTION_VIEW?
I have this working with webview but loading the html page inside the app really degrades the user experience.
Use FileProvider to create a nice content scheme uri for your html file.
Or if you placed your html files at design time in your project assets folder then your html file is in assets resource when your app runs and you can display it in a WebView component. Webview load html from assets directory
If you do not want to use a WebView in your app use your own FileProvider/ContentProvider to serve files from assets.
You could also copy the file from assets resource to local storage and then use FileProvider with ACTION_VIEW to serve your html.
I'm trying to view my HTML file on the android mobile browser.
My HTML file has the following code:
<html>
<head>
<title>Testy</title>
</head>
<body>
<h1>Captain is Here</h1>
<img src='captain.jpg' width='30%' alt='Testy'/>
</body>
</html>
The image(captain.jpg) is also in the same folder where the HTML page is located. I'm getting the image on the computer browser but when I'm trying to open it in mobile browser image, not showing. I tried giving different file paths. How can I achieve this by giving a universal path by that a mobile that containing these HTML files should open in respective browsers and render images too?
1- your image size is too large try an image with a smaller size or resize or format your image into a smaller size.
try src-'./caption.jpg' instead of src='caption.jpg'
Opening through directry location of Html in Chrome Solve this.
For that get html file location directry in details by file manager
Example storage/emulated/0/web_folder/file.html and copy it
After that
1.Open chrome
2.type file:/// and Paste directry location
3."file:///storage/emulated/0/web_folder/file.html" Now it can run with image
Ensure that storage permission allowed to chrome
For that Open permission settings for chrome and allow storage permission
that's where mhtml and URLs get used!!!
Have you ever downloaded a page using chrome in android? if so you would have known about mhtml
MHTML, an initialism of "MIME encapsulation of aggregate HTML documents", is a web page archive format used to combine, in a single computer file, the HTML code and its companion resources (such as images, Flash animations, Java applets, and audio and video files) that are represented by external hyperlinks in the web page's HTML code
to convert your html to mhtml you may use some tools but you can also do it manually .
converting need not rename the file as index.mhtml but use all necessary files as hyperlinks
you may use image hosting sites like imgur or imgbb or even firebase if your familiar.
after you host get the link by
right-clicking image
open image in new tab
copy link
use it(link) instead of captain.jpg
Here is an example of how to get link using imgbb
Is there a way to load an html page on a webview without using a web server?
In my android application I have a web server because the user can save the web pages he wants and he can access them offline later.
I tried the assets folder but I cannot modify it at runtime. I can just read files I´ve put there.
To load online or offline I use the methods of the webview:
browser is the webview.
browser.setWebViewClient(new client());
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.getSettings().setUseWideViewPort(true);
browser.loadUrl(url);
The url can be :
http://stackoverflow.com a normal one
or
http://localhost:8080"; //the web server inside the app
or from assets folder
files:///android_asset/file.html
But how I said, the use can save many pages and see it when he wants.
Is there a method to load the page saved in internal memory on the webview?
URL =this.getApplicationContext().getFilesDir()+"/www/file.html";
browser.loadUrl(URL);
For example: webview.loadString(); ?
Something like browser.loadUrl(Uri.fromFile(f).toString()) should work, for a File object named f pointing to your desired file. Uri.fromFile() will give you a Uri with the correct scheme; toString() gives you the string representation to hand to the WebView. I use this (or a variation) to read files on external storage. AFAIK, it should work fine for files on internal storage as well.
I would like to load a html file (with pictures and videos) on a Android tablet, however, there is no internet connection, thus I cannot use url for the pictures and stuff....
Is there any free web servers that allow me to do so (displaying the web content at local host)?
Or is there any way I can do it by writing a android app for it?
Thanks!!
Webview's LoadURL method can take local content. using the file uri retrieving the content from your assets and render it that way. Assuming you follow the standard folder structure for a basic HTML website.
And why would you want to ask about "free web servers" if you don't even have a internet connection... All the content has to be loaded locally on the app or downloaded on the fly (assuming you don't download too much.. if its too much then you are better off packaging it in the app).
Without Internet your html page is static page.
You can store the html file in your local assets folder then:
url = "file:///android_asset/" + mName + ".html";
mWebView.loadUrl(url);
and if you also want to show the images in your web page change the source path of images in source code of html page to local folder and pate all the images to that folder.
I am developing an Android application in which I have a WebView. I want to save the entire webpage loaded in this webview (Html + all resources + images + icons etc) into a folder and zip it and upload it to a server.
If you use WebView's saveWebArchive, then it is saved in archive format. How do I get the Html and images back from this archive? Is there any documentation for the format of this archive?
If I use addJavaScriptInterface to get the html as described here, I still have to copy the images and other resources from the webview cache dir (/data/data/your app package/cache/webviewCache/). However I did not find webview cache dir (/data/data/your app package/cache/webviewCache/) in Icecream Sandwich.
Is there a way to save the entire webpage displayed in webview along with resources in Android?
Thanks
If anyone is still interested, there is an open source project here that can read WebView's archive format (saved with saveWebArchive) and display it back in a WebView.
https://github.com/gregko/WebArchiveReader
I couldn't find details of the actual format yet.
You can use HTML 5 to store pages in the cache and load from the cache itself.
Caching is easy using HTML5, all you have to do is to make and refer a manifest file that holds information on what all elements need to be cached.
http://www.html5rocks.com/en/tutorials/appcache/beginner/
And here is a good way to implement the cache feature in your webview. http://alex.tapmania.org/2010/11/html5-cache-android-webview.html
Aman Gautam