android中的设备屏幕截图

Posted

技术标签:

【中文标题】android中的设备屏幕截图【英文标题】:device screen shots in android 【发布时间】:2013-11-16 04:37:14 【问题描述】:

我在布局中使用 webview,其中包含一个按钮,单击该按钮时,我尝试捕获设备屏幕截图,单击在我的班级中调用 javascript 方法,

public void screenShots()
        

         v1 = RelativeLayout.getRootView();
              v1.setDrawingCacheEnabled(true);
            Bitmap bm = v1.getDrawingCache();
            BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
            v1.setDrawingCacheEnabled(false);

            image.setBackgroundDrawable(bitmapDrawable);


        

RelativeLayout 是包含 webview 的布局 当我运行并单击按钮拍摄屏幕截图时, 它抛出错误

11-05 03:08:09.761: W/webview(21152): java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
11-05 03:08:09.761: W/webview(21152):   at android.webkit.WebView.checkThread(WebView.java:9955)
11-05 03:08:09.761: W/webview(21152):   at android.webkit.WebView.getSettings(WebView.java:4314)
11-05 03:08:09.761: W/webview(21152):   at android.webkit.WebView.onDraw(WebView.java:4498)
11-05 03:08:09.761: W/webview(21152):   at android.view.View.draw(View.java:11007)
11-05 03:08:09.761: W/webview(21152):   at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
11-05 03:08:09.761: W/webview(21152):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
11-05 03:08:09.761: W/webview(21152):   at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
11-05 03:08:09.761: W/webview(21152):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
11-05 03:08:09.761: W/webview(21152):   at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
11-05 03:08:09.761: W/webview(21152):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
11-05 03:08:09.761: W/webview(21152):   at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
11-05 03:08:09.761: W/webview(21152):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
11-05 03:08:09.761: W/webview(21152):   at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
11-05 03:08:09.761: W/webview(21152):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
11-05 03:08:09.761: W/webview(21152):   at android.view.View.draw(View.java:11010)
11-05 03:08:09.761: W/webview(21152):   at android.widget.FrameLayout.draw(FrameLayout.java:450)
11-05 03:08:09.761: W/webview(21152):   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2302)
11-05 03:08:09.761: W/webview(21152):   at android.view.View.buildDrawingCache(View.java:10724)
11-05 03:08:09.761: W/webview(21152):   at android.view.View.getDrawingCache(View.java:10505)
11-05 03:08:09.761: W/webview(21152):   at android.view.View.getDrawingCache(View.java:10470)
11-05 03:08:09.761: W/webview(21152):   at com.curioussolutions.tashpatti.TashPatti$JavaScriptInterface.screenShots(TashPatti.java:90)
11-05 03:08:09.761: W/webview(21152):   at android.webkit.WebViewCore.nativeTouchUp(Native Method)
11-05 03:08:09.761: W/webview(21152):   at android.webkit.WebViewCore.nativeTouchUp(Native Method)
11-05 03:08:09.761: W/webview(21152):   at android.webkit.WebViewCore.access$3900(WebViewCore.java:56)
11-05 03:08:09.761: W/webview(21152):   at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1388)
11-05 03:08:09.761: W/webview(21152):   at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 03:08:09.761: W/webview(21152):   at android.os.Looper.loop(Looper.java:137)
11-05 03:08:09.761: W/webview(21152):   at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:737)

怎么办,有没有参考

【问题讨论】:

你可以试试这个。 [截图][1][1]:***.com/questions/2661536/… 【参考方案1】:

创建一个文件并将图像存储在 SD 卡中,如下所示:

     // Declaration :

     private Activity activity;

     // Initialization :

     activity = MainActivity.this;


    activity.runOnUiThread(new Runnable() 

        @Override
        public void run() 
            // TODO Auto-generated method stub

       File photo = new File(Environment.getExternalStorageDirectory()
                    + "/.SomeWhereInSD", "photoToShare.png");
            saveImage.setDrawingCacheEnabled(true);
            saveImage.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            saveImage.post(new Runnable() 
                public void run() 
                    Bitmap bitmap = null;
                    System.gc();
                    bitmap = Bitmap.createBitmap(saveImage.getDrawingCache(true));
                    saveImage.setDrawingCacheEnabled(false);
                    OutputStream outStream = null;


                    try 
                        outStream = new FileOutputStream(photo.getAbsolutePath());
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                        outStream.flush();
                        outStream.close();

                        myImage.setImageBitmap(myBitmap);


                     catch (FileNotFoundException e) 

                     catch (IOException e) 

                     catch (OutOfMemoryError out) 

                    
                ;
            );
        
        );

【讨论】:

【参考方案2】:

您是否在您的 java 代码中使用了 javaScript 接口?曾经遇到过类似的情况。 javascript 界面(在 webViewClient 中)帮助了我。

我用这个作为

private void openWebviewDialog(String days)

    dialog = new Dialog(MainActivity.GLOBAL_CONTEXT);
    dialog.setContentView(R.layout.simple_webview);
    WebView wb = (WebView) dialog.findViewById(R.id.simple_webview);
    WebSettings webSettings = wb.getSettings(); 
    wb.setWebViewClient(new MyWebViewClient());
    webSettings.setJavaScriptEnabled(true);
    wb.addJavascriptInterface(this, "javaScriptInterface");
    String url = "http://www.google.com" // your url here
    String title = "Hello";

    wb.loadUrl(url);
    dialog.setCancelable(true);
    dialog.setTitle(title);
    dialog.show();


class MyWebViewClient extends WebViewClient 

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    
        if(url.contains("openExternal"))
         
            return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
         
        else 
        
            view.loadUrl(url); // Stay within this webview and load url
            return true;
        
    

    @Override
    public void onPageFinished(WebView view, String url) 
    
        Utils.hideSpinner();
        super.onPageFinished(view, url);
    

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) 
    
        Utils.showSpinner();
        super.onPageStarted(view, url, favicon);
    


@JavascriptInterface
public void passResult(String status, String message) 

   // Do as required. This method is called from JavaScript code of webView

这个 javaScriptInterface 是从 webView 调用的

<p>please wait while we are verifying your payment....</p><script type='text/javascript' language='javascript'>javaScriptInterface.passResult('error','Your transaction failed');</script>

【讨论】:

ya m 使用 javaScript 界面,但 javascript 界面(在 webViewClient 中)???????????? 我刚刚编辑了我的答案,看看这是否对你有帮助。

以上是关于android中的设备屏幕截图的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式获取 android 设备屏幕截图? [复制]

Android:如何在有根设备上获取帧缓冲区(屏幕截图)?

使用 Java 代码在 Android 上捕获屏幕截图 [重复]

React Native (Android) 中的屏幕截图

在 Android 4.0+ 上检测屏幕截图尝试

利用monkeyrunner实现Android屏幕连续截图