2014-12-05 72 views
0

我想將網頁截圖截圖到SD卡。Android - 如何在點擊時抓取網頁視圖的快照

我使用下列獲得網頁中的WebView

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; 
}} 

感謝您的幫助。

問候,

回答

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; 
    } 
+0

fl無法解析爲變量 – FAAD 2014-12-05 06:16:38

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. 
+0

我想在點擊觸發... – FAAD 2014-12-05 05:43:59

+0

採用B到位newbitmap – 2014-12-05 06:17:20

+0

的佛羅里達州是一個文件對象使用代碼\文件FLA =新的文件(環境。 getExternalStorageDirectory(), 「Screenshot」+「.jpg」); – 2014-12-05 06:18:39

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; 
    } 
+0

錯誤上fl = .. – FAAD 2014-12-05 06:11:12

+0

和newbitmap .. – FAAD 2014-12-05 06:11:42