Android - 如何在点击时拍摄网页视图的快照

时间:2014-12-05 05:24:28

标签: android webview

我想将webview截图带到sdcard。

我正在使用以下内容获取网页浏览中的网页

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mService = new BluetoothService(this, mHandler);
    setContentView(R.layout.main);
    web = (WebView) findViewById(R.id.webView1);
    setUpWebView();
    web.loadUrl("http://www.domain.com");
}

我想将快照代码放在点击功能的后面。

class ClickEvent implements View.OnClickListener {
    public void onClick(View v) {
         ???????????????????????
   }}

请帮助我,因为我在过去两天尝试了不同的代码,但没有运气

我尝试了以下代码建议,但这是关闭应用程序。

w = new WebView(this);
   w.setWebViewClient(new WebViewClient()
    {
        public void onPageFinished(WebView view, String url)
        {
                Picture picture = view.capturePicture();
                Bitmap  b = Bitmap.createBitmap( picture.getWidth(),
                picture.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas c = new Canvas( b );

                picture.draw( c );
                FileOutputStream fos = null;
                try {

                    fos = new FileOutputStream( "mnt/sdcard/yahoo.jpg" );
                        if ( fos != null )
                        {
                            b.compress(Bitmap.CompressFormat.JPEG, 100, fos);

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

               }
      }
  });

setContentView(w);
w.loadUrl("http://search.yahoo.com/search?p=android");
 }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}

感谢您的帮助。

的问候,

3 个答案:

答案 0 :(得分:0)

Use this Code to get the screen shot of web view


public File getBitmapFromwebchartView(WebView view2) {
        if (view2 != null) {
            view2.setDrawingCacheEnabled(true);
            Bitmap b = view2.getDrawingCache();
            if (b != null) {


                try {

                    fl = new File(Environment.getExternalStorageDirectory(),
                            "Realitycheck" + ".jpg");

                    // write the bytes in file
                    FileOutputStream fo;

                    fo = new FileOutputStream(fl);

                    newbitmap.compress(CompressFormat.JPEG, 95, fo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return fl;
    }
Use this code and get the required file stored in sdcard.

答案 1 :(得分:0)

File file;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mService = new BluetoothService(this, mHandler);
    setContentView(R.layout.main);
    web = (WebView) findViewById(R.id.webView1);
    setUpWebView();
    web.loadUrl("http://www.domain.com");
}
class ClickEvent implements View.OnClickListener {
    public void onClick(View v) {
    file=  getBitmapFromwebchartView(web);
   }}
public File getBitmapFromwebchartView(WebView view2) {
        if (view2 != null) {
            view2.setDrawingCacheEnabled(true);
            Bitmap b = view2.getDrawingCache();
            if (b != null) {


                try {

                    fl = new File(Environment.getExternalStorageDirectory(),
                            "Realitycheck" + ".jpg");

                    // write the bytes in file
                    FileOutputStream fo;

                    fo = new FileOutputStream(fl);

                    newbitmap.compress(CompressFormat.JPEG, 95, fo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return fl;
    }

答案 2 :(得分:0)

Sry replace the function with this code

public File getBitmapFromwebchartView(WebView view2) {
        if (view2 != null) {
            view2.setDrawingCacheEnabled(true);
            Bitmap b = view2.getDrawingCache();
            if (b != null) {


                try {

                    fl = new File(Environment.getExternalStorageDirectory(),
                            "Realitycheck" + ".jpg");

                    // write the bytes in file
                    FileOutputStream fo;

                    fo = new FileOutputStream(fl);

                    b.compress(CompressFormat.JPEG, 95, fo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return fl;
    }