是否有可能在Android中获取WebView的显示部分的图像?

时间:2012-03-10 14:24:21

标签: android image webview

当Android中的WebView显示网页时,实际显示的部分位于视图框中。是否有可能有它的形象?

1 个答案:

答案 0 :(得分:0)

你可以进行屏幕转储。 This link具体说明了如何将webview的内容保存到位图。

代码:

package com.testWebView; 
import java.io.FileOutputStream; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Picture; 
import android.os.Bundle; 
import android.util.Log; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
public class testWebView extends Activity 
{ 
        WebView w = null; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        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( "/sdcard/yahoo_" + 
System.currentTimeMillis() + ".jpg" ); 
                        if ( fos != null ) 
                        { 
                                b.compress(Bitmap.CompressFormat.JPEG, 90, fos ); 
                                fos.close(); 
                        } 
                } catch( Exception e ) 
                        { 
                        //... 
                        } 
                } 
          }); 
        setContentView( w ); 
        w.loadUrl( "http://www.yahoo.com"); 
    } 
} 

Android Manifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.testWebView" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/ 
app_name"> 
        <activity android:name=".testWebView" 
                  android:label="@string/app_name"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category 
android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
    </application> 
    <uses-sdk android:minSdkVersion="3" /> 
<uses-permission android:name="android.permission.INTERNET"></uses- 
permission> 
</manifest>