2012-04-15 36 views

Trả lời

2
WebView webView = new WebView(this); 
//your image is in webview 

Picture picture = webView.capturePicture(); 
Canvas canvas = new Canvas(); 
picture.draw(canvas); 
Bitmap image = Bitmap.createBitmap(picture.getWidth(), 
picture.getHeight(),Config.ARGB_8888); 
canvas.drawBitmap(mimage, 0, 0, null); 
if(image != null) { 
    ByteArrayOutputStream mByteArrayOS = new 
    ByteArrayOutputStream(); 
    image.compress(Bitmap.CompressFormat.JPEG, 90, mByteArrayOS); 
    try { 
     fos = openFileOutput("image.jpg", MODE_WORLD_WRITEABLE); 
     fos.write(mByteArrayOS.toByteArray()); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

thử trên để chụp hình ảnh từ WebView

+2

mã bạn gửi chỉ được sử dụng để có được những ảnh chụp màn hình của webview? –

+0

Tôi muốn lưu mọi hình ảnh webview hiển thị –

+1

Tại sao chấp nhận câu trả lời nếu đó không phải là những gì bạn đã hỏi? –

1

Sau đó, bạn phải thiết lập một WebViewClient để WebView của bạn và ghi đè shouldOverrideUrlLoadingonLoadResource phương pháp. Hãy để tôi cung cấp cho bạn một ví dụ đơn giản:

WebView yourWebView; // initialize it as always... 
// this is the funny part: 
yourWebView.setWebViewClient(yourWebClient); 

// somewhere on your code... 
WebViewClient yourWebClient = new WebViewClient(){ 
    // you tell the webclient you want to catch when a url is about to load 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url){ 
     return true; 
    } 
    // here you execute an action when the URL you want is about to load 
    @Override 
    public void onLoadResource(WebView view, String url){ 
     if(url.equals("http://cnn.com")){ 
      // do whatever you want 
      //download the image from url and save it whereever you want 
     } 
    } 
} 
0

tôi đã sử dụng mã từ trên và nó "làm việc" nhưng nó đã tạo ra hình ảnh đen chỉ để sau một vài giờ đây là sự điều chỉnh của tôi, bây giờ nó viết trên thẻ sd bên ngoài không có rủi ro deprecation hoặc các vấn đề con đường ...

public void captureWV() { 
    Picture picture = webview.capturePicture(); 
    Bitmap image = Bitmap.createBitmap(picture.getWidth(),picture.getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(image); 
    picture.draw(canvas); 
    if (image != null) { 
     ByteArrayOutputStream mByteArrayOS = new ByteArrayOutputStream(); 
     image.compress(Bitmap.CompressFormat.JPEG, 90, mByteArrayOS); 
     try { 
      File sdCard = Environment.getExternalStorageDirectory(); 
      File dir = new File(sdCard.getAbsolutePath()); 
      File file = new File(dir, "filename.jpg"); 
      FileOutputStream fos = new FileOutputStream(file); 
      fos.write(mByteArrayOS.toByteArray()); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

cũng ở đây là sự khởi đầu của MainActivity tôi

public class MainActivity extends Activity { 
private static final String URL = "http://punto.gt"; //your website 
WebView webview; 
// your code here 
} 
+0

Ảnh chụp màn hình được chụp luôn màu đen. :( –

Các vấn đề liên quan