试图在android webview中附加图像

时间:2012-05-23 10:01:25

标签: javascript android function webview

所以我一直试图找到使用javascript在webview中附加图像的最简单方法。我自己写了html并包含了我自己的函数,看起来像这样:

<script type="text/javascript">

function image(src) {
    var img = document.createElement("IMG");
    img.src = src;
    document.getElementById('image').appendChild(img);
}
</script>

我发现我不能使用     myview.loadurl(JavaScript的:图像( 'line1.png')); 所以我想弄清楚我怎么能做到这一点。我正在考虑将此函数降级为我可以适应loadurl()的一行代码,但我似乎也遇到了错误。我听说过使用addJavascriptInterface,但它似乎很复杂,我不确定它是否适合我想要的。那么我最好的行动方式是什么?

1 个答案:

答案 0 :(得分:2)

这很简单。请尝试以下

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+"\")");
                            }
                    });
            }
    }
}

的index.html:

<script type="text/javascript">
 function getimage()
 {
   Android.ProcessJavaScript("image",null); 
 }

function image(src) {
    var img = document.createElement("IMG");
    img.src = src;
    document.getElementById('image').appendChild(img);
}
</script>

并添加以下内容,

<body onload="getimage()" >

传递更多图片:

function image(src) {

        //var img = document.createElement("IMG");
        //img.src = src;
       // document.getElementById('image').appendChild(img);
            images = eval('(' + src+ ')');
            for (i = 0; i < images.length; i++) {    
            var img = document.createElement("IMG");
            img.src = images[i];            
              document.getElementById('image').appendChild(img);
            }
    }

并在加载URL中传递String数组而不是String,如

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<String> url=new ArrayList<String>();
                                    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+"\")");
                                }
                        });
                }
        }

多次调用图片脚本:

  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+"\")");

}