试图追加在android的web视图图像 [英] Trying to append an image in android webview

查看:160
本文介绍了试图追加在android的web视图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直都试图找到使用JavaScript的网页视图追加图像的最简单方法。我写出来的HTML我自己和我列入自己的函数,它看起来是这样的:

So ive been trying to find the simplest way to append an image in a webview using javascript. i wrote out the html myself and included my own function that looked something like this:

<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'));
于是我试着去弄清楚如何我可以做到这一点。我在想这就让这个功能code,我可以适合使用loadURL()中的一个线,但我似乎越来越围绕一个错误也是如此。香港专业教育学院听说过用addJavascriptInterface,但它是否适合我想要什么了看似复杂和IM不能确定。因此,whats我最好的行动方针?

I found out that i cant just use myview.loadurl(javascript:image('line1.png')); So im trying to figure out how i can do this. I was thinking about relegating this function to one line of code i can fit in loadurl(), but i seem to be getting an error around that as well. Ive heard of using addJavascriptInterface but its seems complex and im not sure if it fits what i want. So whats my best course of action?

推荐答案

这就是这么简单。请尝试以下

That is so simple. Try the following

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的:

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);
            }
    }

和你传递字符串数组,而不是字符串负载网址像

and you pass String array instead of String in load url like

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

}

这篇关于试图追加在android的web视图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆