Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That is so simple. Try the following</p> <pre><code>import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.webkit.WebView; public class StackOverFlowActivity extends Activity { private Handler mHandler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WebView view=(WebView)findViewById(R.id.webView1); view.getSettings().setJavaScriptEnabled(true); view.loadUrl("file:///android_asset/index.html"); view.addJavascriptInterface(new MyJavaScriptInterface(), "Android"); } final class MyJavaScriptInterface { public void ProcessJavaScript(final String scriptname, final String args) { mHandler.post(new Runnable() { public void run() { String url="file:///android_asset/img.jpg"; webview.loadUrl("javascript:image(\""+url+"\")"); } }); } } } </code></pre> <p>index.html:</p> <pre><code>&lt;script type="text/javascript"&gt; function getimage() { Android.ProcessJavaScript("image",null); } function image(src) { var img = document.createElement("IMG"); img.src = src; document.getElementById('image').appendChild(img); } &lt;/script&gt; </code></pre> <p>and add the following,</p> <pre><code>&lt;body onload="getimage()" &gt; </code></pre> <p><strong>To pass more images:</strong></p> <pre><code>function image(src) { //var img = document.createElement("IMG"); //img.src = src; // document.getElementById('image').appendChild(img); images = eval('(' + src+ ')'); for (i = 0; i &lt; images.length; i++) { var img = document.createElement("IMG"); img.src = images[i]; document.getElementById('image').appendChild(img); } } </code></pre> <p>and you pass String array instead of String in load url like</p> <pre><code>final class MyJavaScriptInterface { public void ProcessJavaScript(final String scriptname, final String args) { mHandler.post(new Runnable() { public void run() { //String url="file:///android_asset/img.jpg"; //webview.loadUrl("javascript:image(\""+url+"\")"); ArrayList&lt;String&gt; url=new ArrayList&lt;String&gt;(); url.add("file:///android_asset/img1.jpg"); url.add("file:///android_asset/img2.jpg"); url.add("file:///android_asset/img3.jpg"); webview.loadUrl("javascript:image(\""+url+"\")"); } }); } } </code></pre> <p><strong>To call image script more times:</strong></p> <pre><code> final class MyJavaScriptInterface { public void ProcessJavaScript(final String scriptname, final String args) { mHandler.post(new Runnable() { public void run() { // you can load url anywhere in this application. check below to call this inside the other function. callimageFirstTime(scriptname,args); // check below to loadurl in other class Test.java Test newtest=new Test(); newtest.methodtoloadurl(scriptname,args); } }); } } public void callimageFirstTime(String script, String arguments) { // add images with String array and pass here webview.loadUrl("javascript:image(\""+url+"\")"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload