跳至主要内容

博文

目前显示的是标签为“shot”的博文

Android screenShot功能的实现

谷歌就是Android5.0版本时候发放了现在的这个接口 android.media.projection.MediaProjection。使用该接口,第三方应用程序无需获取系统root权限也可以直接进行屏幕截图操作了。查询其官方api可知,该接口主要用来“屏幕截图”操作和“音频录制”操作。好吧,直接上干货吧。 一、使用方法。 首先用参数MEDIA_-PROJECTION_SERVICE调 用Context.getSystemService(),得到MediaProjectionManager类别实例; 其次,调用 createScreenCaptureIntent ()得到一个Intent;再次,使用startActivityForResult()启动屏幕捕捉; 最后,将结果返回到 getMediaProjection()上,获取捕捉数据。 二、Demo案例。 1.导入接口 import android.media.projection.MediaProjection ; import android.media.projection.MediaProjectionManager ; 2.实例化MediaProjectionManager mMediaProjectionManager = (MediaProjectionManager) getSystemService( MEDIA_PROJECTION_SERVICE ) ; 3.利用MediaProjectionManager类实例的功能函数createScreenCaptureIntent()生成intent,为接下来的的抓取屏幕做准备 Intent captureIntent = mMediaProjectionManager .createScreenCaptureIntent() ; startActivityForResult(captureIntent , REQUEST_CODE ) ; 4. 在onActivityResult()中获取返回值 protected void onActivityResult ( int requestCode , int resultCode , Intent data) { MediaProjection me...