Android WebView loading dropbox shortened URLs - android

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().

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

Mobile Application based on the website

Im new with Android. My task is to create a mobile application based on a responsive WordPress website. I would like to be able to download some of the content of this site. Website uses HTTPS. In the first, most primitive version, I tried to use WebView. I wanted to use the following: WebView load website when online, load local file when offline. Unfortunately, only the white page appeared and nothing more. In the case of "Google.com link" a page has been loaded in an external browser (Oreo Android).
Any suggestion how should I start? The easiest way would be to download an HTML page or have access to files on the server. Unfortunately, I do not have that access.
As always -1 without even trying to help....
As I said load from cache when Internet is disabled not working. Work only WebView (without login, because CORS). So how should I face it?

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.

Android WebView Disable <Audio> Tag

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.

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

Categories

Resources