2017-09-25 46 views
0

我在上面的Api 21中試圖捕獲webview的屏幕截圖,它只捕獲屏幕上可見的webview的內容,其他部分也被捕獲但沒有任何內容。意味着它顯示出白色空間。在上面的API中採用Webview的屏幕截圖21

我正在使用下面的代碼進行截圖。

public static String getScreenShotOf(View view, Context context) { 
    try { 
     DisplayMetrics displayMetrics = new DisplayMetrics(); 
     ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
     view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); 
     view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); 
     view.buildDrawingCache(); 
     Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); 

     Canvas canvas = new Canvas(bitmap); 
     view.draw(canvas); 

     return storeImage(context, bitmap); 
    } 
    catch (Exception e) { 
     UtillsG.showToast("Error while taking screenshot.", context, false); 
    } 

    return null; 
} 

這些東西在LOLLIPOP或以下版本中都能很好地工作。 查看kitkat和棉花糖設備的截圖。

有人能幫我解決嗎?

任何支持將不勝感激。

enter image description here

enter image description here

回答

4

您可以用root佈局網頁流量,並與下面的代碼試試。

public Bitmap takeScreenshot() { 

View rootView = getWindow().getDecorView().findViewById(R.id.PP_Ll); 
rootView.setDrawingCacheEnabled(true); 
return rootView.getDrawingCache(); 

}

公共無效saveBitmap(位圖的位圖){

String root = Environment.getExternalStorageDirectory().toString(); 
File newDir = new File(root + "/Folder"); 
newDir.mkdirs(); 
Random gen = new Random(); 
int n = 10000; 
n = gen.nextInt(n); 
String fotoname = "Photo-" + n + ".jpg"; 
File file = new File(newDir, fotoname); 
if (file.exists()) 
    file.delete(); 
try { 
    FileOutputStream fos = new FileOutputStream(file); 
    bitmap.compress(CompressFormat.JPEG, 100, fos); 
    fos.flush(); 
    fos.close(); 
    Toast.makeText(getApplicationContext(), 
      "Saved in folder: 'Folder'", Toast.LENGTH_SHORT).show(); 

} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

} }

+0

由於Suheb拉菲克。它符合我的需要。 –

+0

歡迎parambir。請upvote :) –

+0

@SuhebRafique你在第二行的'findViewById()'中傳入的'PP_Ll'是什麼? 我試圖把我的webView的id,但它只是截圖webView上可見的任何東西。 – Chirag