Iam looking for playing media files inside webview Can you please help me out ..what iam missing..I loaded webview as : _webview.loadUrl("file:///android_asset/3gp.html"); and the code inside my html file is as:
<body>
<object width="550" height="400">
<param name="movie" value="sample.3gp">
<embed src="file:///android_asset/sample.3gp" width="550" height="400">
</embed>
</object>
</body>.
Iam not getting the output. Please hellp me out ..Thanks in advance
To play SWF files you have to enable plugins:
_webview.getSettings().setPluginState(PluginState.ON);
and if your .html file uses javascript:
_webview.getSettings().setJavaScriptEnabled(true);
EDIT to answer comment
In your situation you need to use your own webview at the moment links are redirecting to the browser:
_webview.setWebViewClient(new DownloadWebViewClient());
public class DownloadWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false; // Allows your webview to handle clicked links
}
}
Related
I have the following image.
http://example.com/system/files/private/fd41dbdea705d275aa2f5cf09c33731732f82c6d11c1951c5faa570059d52911989001fce5870053c0deeed85c028def20b18502108ad8d5d329ad007f45778c.JPG
It is a private path so a valid cookie is needed to get it.
So I am using this code to show it in my webView:
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(myWebView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.setCookie("http://example.com",cookie);
cookieSyncManager.sync();
String getcookie = cookieManager.getCookie("http://example.com");
MyLog.myLog("getcookie "+getcookie);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.loadUrl("http://example.com/system/files/private/fd41dbdea705d275aa2f5cf09c33731732f82c6d11c1951c5faa570059d52911989001fce5870053c0deeed85c028def20b18502108ad8d5d329ad007f45778c.JPG");
It works fine. I can see the image correctly. So I guess the cookie code is working.
Now I need to show the image inside a custom html file (privateimage_test.html):
.....
.....
<div>
<img src="http://example.com/system/files/private/fd41dbdea705d275aa2f5cf09c33731732f82c6d11c1951c5faa570059d52911989001fce5870053c0deeed85c028def20b18502108ad8d5d329ad007f45778c.JPG" style="width:100%">
</div>
.......
Now my java code is the same excepting the last line:
myWebView.loadUrl("file:///android_asset/privateimage_test.html");
In this case I am not seeing my image.
It works fine if I replace the private image with any public image in my html file. So I guess is a cookie problem.
Any help?
Thanks.
If you are trying to load an image from an html stored into /assets, remember that paths of that image must be defined as "file:///android_asset/..."
This is ok :
myWebView.loadUrl("file:///android_asset/privateimage_test.html");
but images defined into privateimage_test.html must have the path
<img src="file:///android_asset/html/<"Image file name">" >
If you have problems loading your image from the .html file stored in /assets
<div>
<img src="http://example.com/system/files/private/myimage.JPG" style="width:100%">
</div>
Remember to have defined the permission for access to internet.
<uses-permission android:name="android.permission.INTERNET"/>
Remember to use urls defined with https:// instead of http:// because you will have problems using Android 9+.
I am building an android application. I am showing external webpage in webview. I have followed these steps:
Load external website in webview. For example example.com, it loads fine in webview
There is an option in example.com site to launch Dialer app on button click. Here is the code.
<div class="center">
<input type="image" src="btn.png" onclick="location.href='tel:0000';"/>
</div>
When I go to example.com from mobile browser and click on button, it can launch Dialer app with phone number
When I click from webview it shows this error
Web page not available
The web page at tel:0000 could not be loaded because:
net::ERR_UNKNOWN_URL_SCHEME
I do not know what is went wrong. Any clue will be helpfull.
NB: I am using real phone number (here it is 0000).
Thank you
You should set a WebViewClient to the WebView and than override shouldOverrideUrlLoading method as follow:
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().toString().startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, request.getUrl());
view.getContext().startActivity(intent);
}
return super.shouldOverrideUrlLoading(view, request);
}
});
I want to display content from remote URL in android web view, which will be return by java script tag. In my case , java script tag return image , which should be load into web view.
Here is code to render image/content from remote URL.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String customHtml = "<script src='http://mytestdomain.com/test.php?w=300&h=250' type='text/javascript'></script>" ;
webView.loadData(customHtml, "text/html", "UTF-8");
}
URL load the content but image are broken and do not display properly. Though i can click on on the image and land into correct link.
I have proper permission
<uses-permission android:name="android.permission.INTERNET"/>
How can i fix it. Any help is appreciate.
I think the issue is with your js code. I tried this code and image is showing fine.
<html>
<body>
<h3>A demonstration of how to access an IMG element</h3>
<img id="myImg" src="http://via.placeholder.com/350x150" alt="The Pulpit Rock" width="304" height="228">
<p>Click the button to get the URL of the image.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myImg").src;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
Also enable DomStorage:
webView.getSettings().setDomStorageEnabled(true);
Broken image issue because presence of protocol-relative url (//) for the image returned by our server. These kind of URLs (//:mytestdomain.com) will force the protocol to be the same as it's parent page, and the app's web view might be missing a base scheme for the image to inherit. I placed proper protocol into url and it working fine now
In webView, I am loading a html file. The file contains text, images and a youtube link. Text and the images are displayed just by loadUrl() method, but video is not being played. How should I get the link and play video.
Format in which link is present in html file is :
<p style="text-align: center; margin: 0px;">
<iframe class="youtube-player" type="text/html" width="300" height="169" src="http://www.youtube.com/embed/4H0fTwtPLfo" allowfullscreen frameborder="0">
</iframe>
</p>
Please guide me.
EDIT
loading html file:
webView.loadUrl("file:///android_asset/"+htmlFile);
webView.setWebViewClient(new VideoWebViewClient());
VideoWebViewClient class:
private class VideoWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
webView.loadUrl(url);
return true;
}
}
EDIT 2
I am able to play video by adding following lines of code:
WebSettings webSetting = webView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
webSetting.setAllowContentAccess(true);
webView.loadUrl("file:///android_asset/"+htmlFile);
webView.setWebViewClient(new VideoWebViewClient());
I want to play video in full screen, but the full screen button freeze the video. Can anyone help?
I Have an application with an html file inside of the assets folder. I want it so that when the user clicks a button, inside of the Android applications activity, the contents of the button get put into a string and passes the string to a string inside of the html file. Taking the contents of the button and passing them to a string inside of the activity I can do. I want to know how can I take the string, that I got from the button, and pass the information to the html file inside of my assets folder?
Are you just trying to display the HTML file with different data based on a button push? You can use %s or %d in your HTML file as well as $1..$n to do multiple replacements:
You have $1%d new $2%s.
Pseudo-Code
getHtmlFromAssets(myfile.html, unReadThings, things) where unReadThings is a number and things is a string.
Make sense?
Conceptually (though you'll be using HTML): http://mobile.tutsplus.com/tutorials/android/android-sdk-format-strings/
Or less Android specific: http://javarevisited.blogspot.com/2011/12/java-string-replace-example-tutorial.html
Resources (including Assets) are meant to be read. You can open a raw file, call a anim file, get a string from String xml file. However, you can't write to a resource.
As AssetsManager documentation states:
This class presents a lower-level API that allows you to open and read raw files that have been bundled with the application as a simple stream of bytes.
Now, what to do? I suggest you look into other types of Storage Options. For your case, I strongly suggest Using the Internal Storage.
addJavaScriptInterface method helps us pass values from a webpage to your android XML view or vice-versa. You can invoke your activity class method form your webpage
On create:
WebView Wv = (WebView) findViewById(R.id.webView);
Wv.getSettings().setJavaScriptEnabled(true);
JavaScriptInterface myJavaScriptInterface
= new JavaScriptInterface(this);
Wv.getSettings().setJavaScriptEnabled(true);
Wv.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
Wv.loadUrl("file:///android_asset/index.html");
then create a class myJavaScriptInterface
class JavaScriptInterface {
Context mContext;
int tpressure;
JavaScriptInterface(Context c) {
mContext = c;
tpressure=0;
}
public int Pressure(){
return tpressure++;
}
}
Sample html file would be:
<head>
<title>jQuery Mobile page</title>
<script src="jquery-1.9.1.min.js"></script>
<script src="jquery.mobile-1.3.2.min.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.mobile-1.3.2.css" />
<script type="text/javascript">
$(document).ready(function () {
setInterval(function() {
$("#tire1").html(AndroidFunction.Pressure())
}, 2000);
});
</script>
</head>
<body>
<div id="some">
Tire Pressure:
</div>
<div id="tire1">
0
</div>
</body>