+ case R.id.save_as_image:
+ // Create a webpage bitmap. Once the Minimum API >= 26 Bitmap.Config.RBGA_F16 can be used instead of ARGB_8888.
+ Bitmap webpageBitmap = Bitmap.createBitmap(currentWebView.getHorizontalScrollRange(), currentWebView.getVerticalScrollRange(), Bitmap.Config.ARGB_8888);
+
+ // Create a canvas.
+ Canvas webpageCanvas = new Canvas(webpageBitmap);
+
+ // Draw the current webpage onto the bitmap.
+ currentWebView.draw(webpageCanvas);
+
+ // Create a webpage PNG byte array output stream.
+ ByteArrayOutputStream webpageByteArrayOutputStream = new ByteArrayOutputStream();
+
+ // Convert the bitmap to a PNG. `0` is for lossless compression (the only option for a PNG).
+ webpageBitmap.compress(Bitmap.CompressFormat.PNG, 0, webpageByteArrayOutputStream);
+
+ // Get a file for the image.
+ File imageFile = new File("/storage/emulated/0/webpage.png");
+
+ // Delete the current file if it exists.
+ if (imageFile.exists()) {
+ //noinspection ResultOfMethodCallIgnored
+ imageFile.delete();
+ }
+
+ try {
+ // Create an image file output stream.
+ FileOutputStream imageFileOutputStream = new FileOutputStream(imageFile);
+
+ // Write the webpage image to the image file.
+ webpageByteArrayOutputStream.writeTo(imageFileOutputStream);
+ } catch (Exception exception) {
+ // Add a snackbar.
+ }
+ return true;
+