Download videos then play the local copy in Android Webview - android

I'm building a thin native wrapper for a web application on Android. I want to keep my HTML & Javascript on the web, as it'll reduce the frequency with which the App needs to be updated.
However, I want to take advantage of SD card storage for storing videos -- my application has several videos that are likely to be played multiple times, so I'd rather download them and play them from the local copy.
What I have already done successfully:
Written a #JavascriptInterface that gets called when the page is being viewed through the app that downloads the videos and saves them to getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath()
Written a script that replaces the src of the html element with a file:// Url once the download is complete.
The Problem
I'm getting a javascript error in the console:
"Not allowed to load local resource: file:///storage/emulated/0/Android/data/path/to/video.mp4"
I presume this is because I have a video element
<video src="file:///......."/>
Which is being served by a different (non local) domain, so webview's security is saying no.
There are several WebSettings methods like setAllowFileAccess(), but they specifically state this only enables JavaScript access to files and not HTML elements such as <img> or (in this case) <video>
Is there any way around this?
If not, what should my alternative approach be?
EDIT:
Could I just use the built in browser cache for this?

Related

PDF in the iframe triggering download automatically

I have below iframe in one of the page in my website.
<iframe src="http://www.pdf995.com/samples/pdf.pdf" id="iframe1">
But when I open same page in android mobiles chrome browser it is automatically triggering download of file which is mentioned in the iframe. How can I prevent auto triggering of download in mobile devices?
You can use this format:
https://docs.google.com/viewerng/viewer?url=http://yourfile.pdf
Just replace http://yourfile.pdf with the link you use.
Ideally, it is important to think on the availability to use the convert/viewer in offline mode!
Why and Because...
If you use an iframe for a stored document on your server and you would made an webapp, you will must play over HTTPS/SSL. (Offline is over https).
It is not sure that the PDF viewer provides a secure url (1), you will probably problems with CORS and others with the « Same origine » to embed the iframe src (2), and finally, it service must delivers online and offline ! So it recommended to embed in application cache all the resources needed to convert the PDF in canvas.
An all in one solution is strongly recommended I think.
Using PDF.js and its worker with a runtime script is a good way...

Is it possible to use a file on External Storage Directory inside android webview?

I have a hybrid app that plays mp3, but I don't want to set all the mp3s inside the assets folder, I want the user to select mp3 that he/she likes to download.
So the question is... Can I use a previously downloaded mp3 inside webview, natively in the app data folder?
Something like this in file:///android_assets/index.html:
<html><body>
<audio src="file:///Android/data/com.packagename/previouslydownloaded.mp3">
</body></html>
Thanks a lot.
Since Android Lollipop you can use files from anywhere because WebView has gained full support for content:// scheme. Older Android versions may also work (for example, MediaPlayer gained Uri support for playback in API 14). Just create a ContentProvider and refer to associated content uris directly in web page.
<html><body>
<audio src="content://com.example.authority/file/path.mp3">
</body></html>
Both files from secondary external storage (accessible only via Storage Access Framework) and your app private files, exposed via FileProvider or something like that, should be accessible via content://.
Note, that writing ContentProviders is slightly tricky business, you may want to look at following question and all it's answers: Is it possible to use content:// as a source for an <audio> element in a WebView

Update offline webpages without updating app

I have an app which contains html files which i am displaying using a webview. This html files are nothing but documentation the users of my app require. My question is, can I update these html files without asking the user to update the entire app? I dont want to replace these html files by hosting the documentaion online.
You will have to do one of these:
Update your app (which is what you don't want)
Host the raw .html files on a web server, and whenever the user opens the app, check if there are new files available and download them.
Simply create a website that shows the .html files and open the
website in your app (which i'm guessing is what you don't want
either)
So you're left with option 2.
To update files without requiring an app update, you will need the app to obtain the HTML content online.
One option, which you mentioned you don't want, is loading a URL into the WebView rather than local HTML files.
The other option would involve hosting the web pages somewhere and the app would download them. The only benefit of this I can see, is that if the device is offline is can use the last obtained HTML file. You could just use caching to achieve this though.
How complicated are the webpages? Using JSON is another method.

How to access local assets from a remote URL in Android WebView?

To load an asset in HTML, I am using the URL file:///android_asset/my_image.png.
It works when I am loading HTML locally, for instance, using a WebView.loadData() method.
However, I am unable to load a local asset from a remote web site, for instance, when I load a page using WebView.loadUrl("http://example.com/my_page.html"). It shows the page, but the image is not loaded.
How can I fix this problem?
UPDATE:
tarkeshwar mentioned that it is not possible because of security reasons. I understand when you open a web page in a browser, then you as a user are unable to control what the web page is accessing. And it is also a bit different to access local file system when you may read sensitive data of the user. Here I would like just to access application assets.
A workaround could be, to download the page and load it into the WebView using loadData() method. But there might be some security switch to allow WebView to access local assets.
UPDATE2:
I need to support Android 2.3+.
You can't link to a local resource from an external page. That is due to security reasons.
See Pekka's answer for a similar question: How to show local picture in web page?
Extend WebViewClient and override shouldInterceptRequest to load the file locally. The urls would all appear remote but you can selectively load which every you need.
http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldInterceptRequest(android.webkit.WebView, java.lang.String)
Also answered here:
webview shouldinterceptrequest example

How to play local video files in a browser

Am looking for the way to play video inside my android browser locally. There may be two or more video's in a local page (like facebook contains video's).
Can anyone please help me out..
Without actually reading about the specifics of the Android built in browser I would safely say, you can't. Well, if you run a local html file it might work, but if you intend to have an external page from which you can play local videos it shouldn't work. I say shouldn't because in the early days of browsers some of them actually did give you access to the local file system. Javascript for example could use file://, something I used myself for image previewing before upload. But this was a major security concern and all modern browsers prevent this.
The difference is if you run a local file or a file on your own local web server. Since then, your local file system is actually part of the web servers file system and the web server can serve your local files.

Categories

Resources