2015-05-23 25 views
1

这里我有一个关于webview的快速问题。如何在android 5.0中捕获webview到位图?

我的要求是捕捉webview并将文件保存在SD卡中,我用下面的代码。

下面代码用于从网页视图生成位图

网页视图为位图:

webview.measure(MeasureSpec.makeMeasureSpec(
       MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED), 
       MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
     webview.layout(0, 0, webview.getMeasuredWidth(), 
       webview.getMeasuredHeight()); 
     webview.setDrawingCacheEnabled(true); 
     webview.buildDrawingCache(); 
     bitmap = Bitmap.createBitmap(webview.getMeasuredWidth(), 
       webview.getMeasuredHeight(), Bitmap.Config.ARGB_8888); 

     Canvas bigcanvas = new Canvas(bitmap); 
     Paint paint = new Paint(); 
     int iHeight = bitmap.getHeight(); 
     bigcanvas.drawBitmap(bitmap, 0, iHeight, paint); 
     webview.draw(bigcanvas); 

    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    webview.setDrawingCacheEnabled(false); 

以下代码将文件保存在存储器中用于该

要保存为文件:

File myDir = new File(Environment.getExternalStorageDirectory(), "Sample"); 
    if (myDir.exists()) 
    { 
    } 
    else 
    { 
     myDir.mkdir(); 
    } 
    String fname = "sample" + ".png"; 
    file1 = new File(myDir, fname); 

    if(bitmap!=null) 
    { 

    try 
    { 
     FileOutputStream out = new FileOutputStream(file1); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 10, out); 
     out.flush(); 
     out.close(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    }} 

但这里的webview加载很好,但没有完全捕获androi d 5.0(棒棒糖),如下图像

enter image description here

显示我怎样才能解决这个问题呢?请给我建议或代码片段。

在此先感谢..

+1

不要测量网页流量,而不是捕捉其中的WebView存在的布局,并将其转换成位图。 – DJphy

回答

1

你需要创建任何网页视图之前调用WebView.enableSlowWholeDocumentDraw()。也就是说,如果您的布局中有任何WebView,请确保在调用下面显示的onCreate()中的setContentView()之前调用此方法。

if (Build.VERSION.SDK_INT >= 21) { 
     webview.enableSlowWholeDocumentDraw(); 
} 

对我来说它的做工精细..

1

U可以得出这样的画布上的观点:

 Bitmap mBitmap; 
     Layout webViewContainer 
     mBitmap = Bitmap.createBitmap(webViewContainer.getWidth(), webViewContainer.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(mBitmap); 
     webViewContainer.draw(canvas); 
+0

感谢您的回复......这里将webviewcontainer设置为null。你提供一些片段 – user512

+0

请检查这个网址http://stackoverflow.com/questions/9161192/webviewfragment-webview-is-null-after-doing-a-fragmenttransaction –

+0

布罗它的你的webViewContainer(urwe webview的布局是present..it可以是你的线性或相对布局根据你的设置),布局只是一个普遍的ViewGroup术语,我在这里采取。 – DJphy

相关问题