pitu pitu - 5 months ago 41
Android Question

device screen shots in android

i am using webview in layout,which contain a button ,on click of that button, i trying to capture device screen shot, on click calling javascript method in my class,
the

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 is the layout which contain webview
when i run & clicks button to take screen shots,
it throws error

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)


what to do,is there any refrence to it

Answer

Create a file and store the image in SD card as like the following:

     // 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) {

                    }
                };
            });
        }
        });