Recently, during the development process, I encountered a need to screen the screen and save it as an image, specifically to capture the view of the webview to save the image.
Method 1: The first idea that comes to mind is to use the () method provided by the SDK:
public void printScreen(View view) { String imgPath = "/sdcard/"; (true); (); Bitmap bitmap = (); if (bitmap != null) { try { FileOutputStream out = new FileOutputStream(imgPath); (, 100, out); } catch (Exception e) { (); } } }
This method has no problem in many cases, such as intercepting imageview, TextView, or even ();, but on the WebView, the webview will cause some missing content in the page to be intercepted, such as opening this with webview (/jellyfish/) interface, there will be problems with the captured pictures, which is manifested as the jellyfish swimming in the web page is not displayed on the captured pictures.
Method 2: Use the service Context.MEDIA_PROJECTION_SERVICE provided by the Android system to perform screenshot operation.
Github address:/miqt/CapWindow
Demo source code download address:CapWindow_jb51.rar
Key part code analysis: ↓
Send a screenshot request
final MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE); Intent intent = (); startActivityForResult(intent, REQUEST_CODE);
Receive the returned result:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { (requestCode, resultCode, data); handleScreenShotIntent(resultCode, data); } private void handleScreenShotIntent(int resultCode, Intent data) { onScreenshotTaskBegan(); final MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE); final MediaProjection mProjection = (resultCode, data); Point size = (this); final int mWidth = ; final int mHeight = ; final ImageReader mImageReader = (mWidth, mHeight, PixelFormat .RGBA_8888, 2); final VirtualDisplay display = ("screen-mirror", mWidth, mHeight, DisplayMetrics.DENSITY_MEDIUM, DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION, (), null, null); (new () { @Override public void onImageAvailable(ImageReader mImageReader) { Image image = null; try { image = (); if (image != null) { final [] planes = (); if ( > 0) { final ByteBuffer buffer = planes[0].getBuffer(); int pixelStride = planes[0].getPixelStride(); int rowStride = planes[0].getRowStride(); int rowPadding = rowStride - pixelStride * mWidth; // create bitmap Bitmap bmp = (mWidth + rowPadding / pixelStride, mHeight, .ARGB_8888); (buffer); Bitmap croppedBitmap = (bmp, 0, 0, mWidth, mHeight); saveBitmap(croppedBitmap);//Save the picture if (croppedBitmap != null) { (); } if (bmp != null) { (); } } } } catch (Exception e) { (); } finally { if (image != null) { (); } if (mImageReader != null) { (); } if (display != null) { (); } (null, null); (); onScreenshotTaskOver(); } } }, getBackgroundHandler()); }
This method is similar to using the mobile phone's system screenshot (volume down key + power button), which can perfectly take down the screen as it is, and modify the save method and even record the screen. However, compared with the first method, its disadvantage is that it has nothing to do with the view on the interface, and when calling this service, a permission confirmation box will pop up. Also, it should be noted that this method can only be applied on Android 5.0 system devices.
Summarize:
In short, these two methods have their own advantages and disadvantages. When using them, you must make a choice based on your actual needs. The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.