epub files does not downloadable [ANDROID] - java

I've set set files to localhost an set the href links for the epub files ..
more-utopia <br>
and I've try to browse from my custom web view .. but when I click to download the link , it appears no download .. and open the file in the browser with custom web view.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView web = (WebView) findViewById(R.id.webview);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://10.0.2.2/epub");
web.setWebViewClient(new myWebView());
}
class myWebView extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
url = "http://10.0.2.2/epub/";
view.loadUrl(url);
return true;
}
}
What I want to do is to download the files and want to set path the download location to user selected location .
Any helpful tips are humbly welcome.

Your code is set up to prevent people from downloading anything. Every time they click the link, you have them load the original Web page again. Try using setDownloadListener() on your WebView instead. Or, change your shouldOverrideUrlLoading() to do what your question says you want it to do.

Related

Problem with restoring webview state after app installation

I have a simple android app with WebView which opens the site main page. I need to save WebView state when the user presses "home" or "main menu" buttons. And then, when user opens app from the homescreen or main menu I need to load this saved state.
Everything works fine except the first launch (after installation of built apk file): if I open app from main menu the WebView restores just fine, but if I try to open it from homescreen it starts to reload. Is there any ways to fix that? Thanks in advance.
P.S. if I install app from Android Studio (not from built apk file) everything works as I expect, so I am confused.
These are methods where I save and restore WebView:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
if (savedInstanceState != null)
webView.restoreState(savedInstanceState);
else
webView.loadUrl(getString(R.string.URL));
}
protected void onSaveInstanceState(Bundle outState)
{
webView.saveState(outState);
super.onSaveInstanceState(outState);
}

Open a link on WebView

I am using WebView on my application.
the WebView contains a web site with link.
When I am clicking on the link, it is open the link on the application(By webView).
How can I open the link with the browser?
Thank you!
Try something like this
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
or if you need to override Webview link click use this
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
This should do the trick

Android webview - webpage renders in a very broken way

I am trying to load this popular podcast in an Android WebView:
http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs
And this is how I render it:
public class PodcastsActivity extends BaseActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(100);
webview.setWebViewClient(new WebViewClient());
setContentView(webview);
webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");
And in my manifest I have the activity defined like this:
<activity
android:name="PodcastsActivity"
android:label="Podcasts"
android:hardwareAccelerated="true"/>
But it renders like this:
Is there an extra setting that needs to be set? Or something I am missing?
Thanks!
I hope you have looked at other answers. Some of the things you may try are:
1) From webview not loading correctly in application question, you may try and enable java script before loading the URL:
webview.setInitialScale(1);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
2) As per Android webview not rendering html content correctly question, if your target is higher than 2.3.3 try adding this in your manifest file.
android:hardwareAccelerated="true"
Update
3) Also check that you have following permission in manifest as direct child to <manifest> tag:
<uses-permission android:name="android.permission.INTERNET" />
Unable to load webpage using WebView in Android
4) You can try shouldOverrideUrlLoading() method as stated here.
Update 2:
As you said that it displays correctly on your browser, there is another possibility of using the above method in case when a certain link is clicked within the app, it opens the default browser. I'm not sure if you would want this but it is a possibility. Something as follows:
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");
Source: WebView link click open default browser
Hope this helps.

Android webview script tag not loading source file

I'm trying to load a javascript file from a script tag in Webview, but it won't load it!
here's the source for the HTML Script tag:
<script type="text/javascript" src="file:///android_asset/game/tetris.js"></script>
And the java file:
WebView webview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/game/tetris.html");
}
I've tried with and without the "file:///android_asset/game/" and it still won't load!
Any ideas? Thanks!
I do the same in my applications with relative paths, and it works fine for me.
In this case I'd simply use src="tetris.js" in the HTML

Web Page Not Available - Loading local HTML on Android

Yo guys, this is bugging me and I can't find a fix for it,
I have a simple activity to launch a WebView and display a HTML file
public class HelpViewer extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page_layout);
WebView browser = (WebView) findViewById(R.id.help_page_webview);
browser.loadUrl("file:///android_asset/help.html/");
}}
The help_page_layout is your standard WebView resting in a LinearLayout - Not problems there.
The help.html is sitting under the /res/raw/ directory. From what I have see online, there shouldn't be a problem here either.
But when the WebView goes to load, it just tells me that the WebPage was not available because help.html couldn't be found.
What am I doing wrong?
This question suggests the file should be in /assets rather than /res/raw. You also have a trailing slash on the end of your URL.

Categories

Resources