Android WebView Disable <Audio> Tag - android

My Android WebView is loading a 3rd party URL.
I.e.http://www.desiringgod.org/resource-library/sermons/gods-good-news-concerning-his-son
It works great, however because of the tag which gets loaded through javascript, the WebView begins downloading the mp3 without user input as soon as the page loads.
I have tried calling WebView.stopLoading() once progress has finished, as well as shouldOverrideUrlLoading(WebView view, String url) to try to catch ending with ".mp3" but it doesn't seem to be able to able to stop it!
My main concern is the amount of data it automatically downloads, if there's any way to stop the download or replace the tags before the page loads that would be great.
Thanks!
UPDATE: actually I found the reason the Audio tags were loading without user prompt is because it is using flash. Once I enabled plugins on webview it stops downloading by itself. However I will still be interested to know how to stop tags within webviews in Android downloading data in background without user knowledge!

I don't know WebView itself, but if the audio tag is being used, you can ask the browser to not download the audio file by setting the preload attribute to none.

Related

Pdf file is not able to load in android webview but able to load in external browser

I have the webpage created with angular js file. There are pdf files inside the webpage we want to download and show.
when clicking the pdf file, it just show loading and nth happened. Also shouldOverrideUrlLoading method is never called. This method is not deprecated.
We tried to load the url in external browser and open the pdf, it successfully download and open the pdf file.
I would like to know is there any webview setting I missed out?
current webview settings in my implementation is as below . Kindly help. thank you
webView?.settings?.builtInZoomControls = true
webView?.settings?.domStorageEnabled = true
webView?.settings?.allowFileAccess = true
webView?.settings?.allowContentAccess = true
You should try this one might be it work by WebViewClient.
Check out this -- https://stackoverflow.com/a/14201778/18858169
PDF is a file format NOT handled by WebView, it's designed to show web content, not some documents
if you really want to show PDF inside WebView then you should use some web application, which can take a PDF file (or URL pointing on some) and render content of your file in a "web way", thus WebView could print it
for example Google have such web app and you can open PDFs in it using some of below URLs (these may changed)
https://docs.google.com/viewer?url=http://customdomain.com/pdffile.pdf
https://docs.google.com/gview?embedded=true&url=http://customdomain.com/pdffile.pdf
https://drive.google.com/viewerng/viewer?embedded=true&url=http://customdomain.com/pdffile.pdf
note that you are basing on 3rd-party service - your PDF may be online, but 3rd-party companys server-side may be down or changed routing and your hardcoded URL prefix stop working
and if you want to show "real" PDF then use PdfRenderer class, which will give you a set of Bitmaps, which can be shown with ImageView, no WebView needed

Does Android Webview timeout iframe objects?

I have an app with a webpage called Avebury.html, and this has some javascript created by a colleague which loads a second HTML file inside it called widget. This has an iframe containing an embedded google map. Both are in the assets folder.
The problem is when trying to load Avebury.html in a webview it times out, and points to not being able to find the widget, when the link is correct.
I have tested linking straight to the widget and opening it in the webview, and it loads as it should. Both pages also work correctly in our iOS app and internet explorer.
The widget does take time to load, and I was wondering if the android webview is timing out the widget too soon, and if so is there a way to make it more lenient?
Thanks in advance.
I have found that android webview does not seam to like loading another HTML file from within the app in an iFrame. Removing the unneeded javascript and putting the iframe straight into the page bypassed the problem.

Download videos then play the local copy in Android Webview

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?

Saving webpage in cache using webview in android

I am working on an application where I load few websites in webview now I want to save webpages so after sometime even if there is not internet user will able to see those pages. But I am confused on how to save whole webpage in cache or any other medium. The main thing is we need to show pages back even if there is not internet.
Has anyone implemented this before. Please provide some demo code as this is my first attempt on cache..
Thank You
The easiest way is save webpages in cache directory or any other(Internal or external storage)
You can get the data of web page using HttpClient.execute() or HttpClient.get() now store that data in .html file also you have to download images or other contents which are bind to that page, Now in your application you have to check for connection if connection not available then load the page which one you saved in storage with file://<location of your webpage..>
EDIT:
I think using HTML5 you can display off-line webpages. (I never tried this, but I referred some blogs on it). Look at this nice post about HTML5 Creating mobile Web applications with HTML 5, Part 3: Make mobile Web applications work offline with HTML 5 Also this
hope this will help you.

Android WebView loading dropbox shortened URLs

I am making an application that creates and allows the user to edit an HTML file by adding notes and / or links to it. It then allows the user to sync it via dropbox so that they can view the file from any other Android device, or from a browser on their computer. In this program I have a WebView which loads the html page and shows it to the user. However if I tell the webview to load the shortened version of the URL(E.g. http://db.tt/DrSmhwq) instead of putting it inside my WebView it opens it in the browser on top of my app. If I use the long version of the URL(E.g. http://dl.dropbox.com/u/5724095/DBNotes.html) it loads it inside my WebView just fine. So I have a few questions: What is the difference between the shortened URLs and the long ones? Is there something about the URL that is causing this behavior, or is it more likely that this is an issue with the Android WebView? And does anyone know if there a way to generate the long versions of the URLs from within the dropbox Android app? Otherwise I am going to have to direct my users to go to the website on their computer and generate the long link and type it in to my app, this does not seem like a good solution.
Edit:
I made a bitly url that points to the longer dropbox url and tried to call wv.loadUrl() on that, it also opened up the browser instead of loading it into the WebView. So I am guessing that this is the default behavior for how the WebView handles redirects. Does anyone know how I can make it load the page that its directed to inside itself instead of a browser window?
What is the difference between the shortened URLs and the long ones?
The server is issuing a redirect, which you are not handling, so the default WebView behavior is invoked -- open the new URL in the browser.
Does anyone know how I can make it load the page that its directed to inside itself instead of a browser window?
Implement a WebViewClient, particularly shouldOverrideUrlLoading(), to handle the redirect. Attach an instance of your WebViewClient to your WebView via setWebViewClient().

Categories

Resources