WebView.capturePicture没有与URL参数 [英] WebView.capturePicture failing with url parameters

查看:128
本文介绍了WebView.capturePicture没有与URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Android的新手警告]

我需要捕获的WebView内容的位图,我碰到一个奇怪的问题。我的做法是注册到的WebView一个WebViewClient,并在 onPageFinished 的我打电话的 capturePicture 的。用一个简单的URL(如 http://www.yahoo.com ),它工作正常。在其他情况下,capturePicture返回一个带有图片的高度和宽度值= 0的页面加载罚款,无论哪种方式。实际的URL我必须使用有相当多的URL参数,我最初以为有任何参数是问题,但事实并非如此。下面的几个示例URL与意见,表示它是否有效与否:

  1. w.loadUrl(http://www.yahoo.com); //是
  2. w.loadUrl(http://search.yahoo.com/search?p=android); //通常不???
  3. w.loadUrl(http://www.yahoo.com?foo=bar); //没了
  4. w.loadUrl(http://www.google.com); //对
  5. w.loadUrl(http://www.google.com?q=android); //对
  6. w.loadUrl(http://www.google.com?foo=bar); //是

第二种情况是特别令人沮丧,因为它似乎不起作用。不过,如果我跑与#5测试程序,再切换网址#2并运行它即可工作。

下面是我创建了一个实际的简化测试的一个片段:

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
 

  W =新的WebView(本);
    w.setWebViewClient(新WebViewClient(){
        公共无效onPageFinished(的WebView的WebView,字符串URL){
            画中画= webview.capturePicture();

            Log.d(高度,+ picture.getHeight());
            Log.d(宽度,+ picture.getWidth());
            位图B = Bitmap.createBitmap(picture.getWidth(),图片
                    .getHeight(),Bitmap.Config.ARGB_8888);
            帆布C =新的Canvas(B);
            picture.draw(C);
        }
    });

    w.getSettings()setJavaScriptEnabled(真)。

    的setContentView(瓦特);

    //w.loadUrl("http://www.yahoo.com); //是
    w.loadUrl(http://search.yahoo.com/search?p=android); //通常不???
    //w.loadUrl("http://www.yahoo.com?foo=bar); //没了
    //w.loadUrl("http://www.google.com); //是的
    //w.loadUrl("http://www.google.com?q=android); //是的
    //w.loadUrl("http://www.google.com?foo=bar); // 是
}
 

有没有人遇到这个问题?但愿我只是作为一个白痴,有一个简单的解决方案或解决方法吗?

解决方案

我只是再次访问该文档页面。 [点击这里] [1]

通知页面加载完成主机应用程序。这种方法被称为唯一的主框架。当onPageFinished()被调用时,呈现的图像可能的不可以来尚未更新。要获得通知新的照片,使用onNewPicture(的WebView,资料图片)

二手图片监听器,我试着用你的样品,它的工作原理。
希望这有助于。

我只是说低于code到你的榜样,并删除WebViewClient

  w.setPictureListener(新PictureListener(){

公共无效onNewPicture(web视图来看,画中画){
            Log.d(TAG,onNewPicture-高度+ picture.getHeight());
            Log.d(TAG,onNewPicture-宽度+ picture.getWidth());
}

    });
 

[1]:<一href="http://developer.android.com/reference/android/webkit/WebViewClient.html#onPageFinished%28android.webkit.WebView">http://developer.android.com/reference/android/webkit/WebViewClient.html#onPageFinished%28android.webkit.WebView, java.lang.String中)

[Android Newbie alert]

I need to capture the contents of a WebView in a BitMap and I've run into a strange problem. My approach is to register a WebViewClient with the WebView, and in onPageFinished I call capturePicture. With a simple URL (e.g. http://www.yahoo.com), it works fine. In other cases, capturePicture returns a Picture with height and width values = 0. The page loads fine, either way. The actual url I have to use has quite a few url parameters and I initially thought having any parameters was the problem, but that's not the case. Here's a few sample urls with comments indicating whether it works or not:

  1. w.loadUrl("http://www.yahoo.com"); //yes
  2. w.loadUrl("http://search.yahoo.com/search?p=android"); // usually not???
  3. w.loadUrl("http://www.yahoo.com?foo=bar"); // nope
  4. w.loadUrl("http://www.google.com"); // yep
  5. w.loadUrl("http://www.google.com?q=android"); // yep
  6. w.loadUrl("http://www.google.com?foo=bar"); // yes

The second case is particularly frustrating as it appears to not work. However, if I run the test app with #5 first, then switching the url to #2 and running it then works.

Here's a snippet of an actual simplified test I created:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    w = new WebView(this);
    w.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView webview, String url) {
            Picture picture = webview.capturePicture();

            Log.d("Height", "" + picture.getHeight());
            Log.d("Width", "" + picture.getWidth());
            Bitmap b = Bitmap.createBitmap(picture.getWidth(), picture
                    .getHeight(), Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);
            picture.draw(c);
        }
    });

    w.getSettings().setJavaScriptEnabled(true);

    setContentView(w);

    //w.loadUrl("http://www.yahoo.com"); //yes
    w.loadUrl("http://search.yahoo.com/search?p=android"); // usually not???
    //w.loadUrl("http://www.yahoo.com?foo=bar"); // nope
    //w.loadUrl("http://www.google.com"); // yep
    //w.loadUrl("http://www.google.com?q=android"); // yep
    //w.loadUrl("http://www.google.com?foo=bar"); // yes
}

Has anyone run into this issue? Hopefully I'm just being an idiot and there's a simple solution or workaround?

解决方案

I just visited the documentation page again. [here][1]

"Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture)."

Used Picture Listener, I tried with your sample and it works.
Hope this helps.

I just added below code to your example and removed WebViewClient

 w.setPictureListener(new PictureListener(){

	public void onNewPicture(WebView view, Picture picture) {
            Log.d(TAG, "onNewPicture- Height"+ picture.getHeight());
            Log.d(TAG, "onNewPicture- Width"+ picture.getWidth());
		}

    });

[1]: http://developer.android.com/reference/android/webkit/WebViewClient.html#onPageFinished%28android.webkit.WebView, java.lang.String)

这篇关于WebView.capturePicture没有与URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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