1

I'm trying to get the whole screen shot programmatically in webview. When I test my code, it returns a white screen.

I've tried different codes, but I couldn't get it done. Sometimes it returns a white screen, sometimes it returns a black screen. I've also added my manifest Internet permission and wrote an external storage permission. My xml file exist only in webview.

I've also tried "sleep" for five seconds but it didn't work.

public static Bitmap loadBitmapFromView(WebView v, int width, int height) 
{
    Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.draw(c);  
    FileOutputStream fos = null;
    try 
    {
        fos = new FileOutputStream("/sdcard/image.jpg" );
    } 
    catch (FileNotFoundException e1) 
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    b.compress(Bitmap.CompressFormat.JPEG, 90, fos);

    try 
    {
        fos.close();
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return b;
}

I call this method in onpageFinished(). By loadBitmapFromView(webView, webView.getWidth(), webView.getHeight()); I ensure loading the whole page. In my opinion, it tries to get a screen shot when webview doesn't load before the call. But I call it in the onpageFinished() method. It runs when webview loads completely.

Other parts of my code
public class Wv extends Activity 
{  

    private static WebView webView;
    CustomWebViewClient webViewClient;
    ProgressDialog mProgressDialog;
      public void onCreate(Bundle savedInstanceState) 
      {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView1);

        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Loading...");

        webView.loadUrl("file:///sdcard/ZipDemo/HTML_Friday.html");
        webViewClient = new CustomWebViewClient();         
        webView.setWebViewClient(webViewClient);     
    }


      private class CustomWebViewClient extends WebViewClient 
        {

            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                   super.onPageStarted(view, url, favicon);

                if(!mProgressDialog.isShowing())
                {
                    mProgressDialog.show();
                }
            }  
            @Override 
            public void onPageFinished(WebView view, String url) {


                   super.onPageFinished(view, url); 
                if(mProgressDialog.isShowing()){
                    mProgressDialog.dismiss();
                }
loadBitmapFromView(webView,webView.getWidth(),webView.getHeight());

         }
       }

2 Answers2

0

Did you enable the View's DrawingCache?

public static Bitmap getBitmapFromView(View view) {
    try {
      view.setDrawingCacheEnabled(true);
      view.buildDrawingCache(true);
      Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
      view.setDrawingCacheEnabled(false); // clear drawing cache
      return bitmap;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

You should use it in the View's post-method:(http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable))

view.post(new Runnable() {
    @Override
    public void run() {

    }
  });
  • it didn't change and when i add this code it returns SPAN EXCLUSIVE cannot have zero length. It also create new jpg file but again it exist only white screen. – Atacan Beköz Jul 20 '15 at 09:00
  • Thanks Philipp but I didn't use post method before and I don't know where I use this method. Can you explain more details ? – Atacan Beköz Jul 20 '15 at 11:08
  • You can use it right after you initialized the view. The Runnable you pass to the post method is called after the view was drawn first (or changed). For example if you set a Bitmap to an ImageView it will not be loaded asap. Instead, you can set the post method and when the post is called the Bitmap you setted will be available. –  Jul 20 '15 at 11:23
  • Can you post the related part of your code? Where do you intialize the View, where do you set the post and what are you doing inside post? –  Jul 20 '15 at 11:31
  • i just saw, you are using a WebView. Did you try the WebView's capturePicture() method? [link] (http://developer.android.com/reference/android/webkit/WebView.html#capturePicture%28%29) mybee this could also help you: [link] (http://stackoverflow.com/questions/7702565/capture-picture-from-android-webview) –  Jul 20 '15 at 11:43
  • I tried capturePicture() method. It didn't work well.For ex. pic=web.capturePicture(); When I use pic.getWidth(); it returns -1 and my code failed. I tried other link now but it isn't worked . – Atacan Beköz Jul 20 '15 at 12:33
0

Problem is solved by deleting CustomWebViewClient class and create new button.onPageFinished() method didn't work well. When I create button problem is solved. This is the code.

public class Wv extends Activity 
{  
    private WebView webView;
    private Button screenShot;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView1);
        screenShot=(Button) findViewById(R.id.button_screenshot);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);

        webView.loadUrl("file:///android_asset/HTML_Friday.html");

      screenShot.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub          

            Picture picture = webView.capturePicture();
            Bitmap b = Bitmap.createBitmap(picture.getWidth(),picture.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);

            File screen = new File(Environment.getExternalStorageDirectory()
                    .toString(), "tt12.jpg");
            if (screen.exists())
                screen.delete();

            picture.draw(c);

            FileOutputStream fos = null;
            try {

                fos = new FileOutputStream(screen);
                if (fos != null) {
                    b.compress(Bitmap.CompressFormat.JPEG, 90, fos);

                    fos.close();
                }
            } catch (Exception e) {
                e.getMessage();

            }
        }
    });
   }    
 }