截取上面的 Webview api 21
Taking Screenshot of Webview in above api 21
我正在尝试上面 Api 21 中的 webview 的屏幕截图,它只捕获屏幕上可见的 webview 的内容,其他部分也被捕获但没有任何内容。表示它显示白色 space.
我正在使用以下代码截取屏幕截图。
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 和 marshmallow 设备的屏幕截图。
有人可以帮我解决这个问题吗?
如有任何支持,我们将不胜感激。
您可以将根布局用作 webview 并尝试使用以下代码。
public Bitmap takeScreenshot() {
View rootView = getWindow().getDecorView().findViewById(R.id.PP_Ll);
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap 位图) {
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();
}
}
}
我正在尝试上面 Api 21 中的 webview 的屏幕截图,它只捕获屏幕上可见的 webview 的内容,其他部分也被捕获但没有任何内容。表示它显示白色 space.
我正在使用以下代码截取屏幕截图。
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 和 marshmallow 设备的屏幕截图。
有人可以帮我解决这个问题吗?
如有任何支持,我们将不胜感激。
您可以将根布局用作 webview 并尝试使用以下代码。
public Bitmap takeScreenshot() {
View rootView = getWindow().getDecorView().findViewById(R.id.PP_Ll);
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap 位图) {
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();
}
} }