English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Hybrid Apps, the sharing methods of the web part are becoming more and more diversified. Common user operation methods include: copying the web link, directly selecting the target application for automatic sharing, etc. Among them, the screenshot behavior is increasingly becoming one of the interactive ways that enrich user operations and are favored by users. We can see this feature in many content community applications. This article summarizes the implementation methods of WebView screenshot in Android applications.
As a special control, WebView cannot obtain screenshots like other system Views or screen capture methods (usually for capturing long screenshots). For example:
public static Bitmap getScreenShot(View view){ View screenView = view.getRootView(); screenView.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache()); screenView.setDrawingCacheEnabled(false); return bitmap; }
If the above code is used on a WebView, it will result in a screenshot with incomplete content. In fact, the WebView system itself provides corresponding APIs to obtain the Bitmap object.
private Bitmap captureWebView(WebView webView){ Picture picture = webView.capturePicture(); int width = picture.getWidth(); int height = picture.getHeight(); if (width > 0 && height > 0) { Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); picture.draw(canvas); return bitmap; } return null; }
After obtaining the Bitmap object, this code can be used to save it to the device's storage card:
private void saveBitmap(Bitmap bitmap){ File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()} + ".jpg"); try { FileOutputStream fos = new FileOutputStream(file); bitmap.compress(CompressFormat.JPEG, 8(0, fos); fos.flush(); fos.close(); } catch (java.io.IOException e) { e.printStackTrace(); } }
Just two simple steps, and the job is done. However, when you are in Android 5Version 0.0 and higher version systems, when operating, you will find that the screenshot is not complete. Although the width and height of the image meet the actual requirements, the content only includes the WebView content within the current screen display area.
The reason lies in that, in order to reduce memory usage and improve performance, from Android 5Starting from version 0.0, the system can intelligently select part of the Html document for rendering. Therefore, by default, we can only capture the content of the WebView within part of the screen display area, which leads to the above problem.
However, the system also provides corresponding APIs to modify this default optimization behavior. The code is very simple:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { WebView.enableSlowWholeDocumentDraw(); }
It should be noted that this code must be added before the WebView instance is created. If using Activity, it is before the setContentView() method.
Although the capturePicture() method can already capture the WebView screenshot, but up to API 19 This method has been deprecated by the system. Instead, use the onDraw() method to obtain the Bitmap object.
private Bitmap captureWebView(WebView webView){ float scale = webView.getScale(); int width = webView.getWidth(); int height = (int) (webView.getHeight()) * scale); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); webView.draw(canvas); return bitmap; }
What needs to be mentioned again is that the getScale() method from API 17 The method of obtaining scale value has also been abandoned by the system. Therefore, another more elegant way to obtain the scale value is:
webView.setWebViewClient(new WebViewClient() { @Override public void onScaleChanged(WebView view, float oldScale, float newScale){ super.onScaleChanged(view, oldScale, newScale); scale = newScale; } });
Finally, in the actual use process, we also need to consider the memory occupation problem of Bitmap, do a good job in exception capture, and prevent the occurrence of OOM.
Summary
The above-mentioned methods of implementing WebView screenshot in Android introduced by the editor are for your reference. If you have any questions, please leave a message, and the editor will reply to you in time. Here, I also want to express my heartfelt thanks to everyone for their support of the Yell Tutorial!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.)