android 两种截屏方式

来源:互联网 发布:软件行业税收优惠政策 编辑:程序博客网 时间:2024/06/08 01:12

截屏的时候会有卡顿,暂时未解决。

第一种(这种截图只会截屏应用内,状态栏是不会截到的):

调用方法

if (ContextCompat.checkSelfPermission(ShareCodeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {                    ActivityCompat.requestPermissions(ShareCodeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 54);                    Log.i("-->", "权限申请");                } else {                    screenshot();                }

在调用之前需要判断权限


 @Override    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {        super.onRequestPermissionsResult(requestCode, permissions, grantResults);        doNext(requestCode, grantResults);    }    private void doNext(int requestCode, int[] grantResults) {        if (requestCode == 54) {            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {                screenshot();            } else {                new AlertDialog.Builder(this)                        .setTitle("权限申请")                        .setMessage("在设置-应用-(应用名称)-权限中启动存储权限,以正常使用此功能")                        .setPositiveButton("去设置", new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialog, int which) {                                Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);                                startActivity(intent);                            }                        })                        .setNegativeButton("取消", null)                        .show();            }        }    }

接下来是截图的具体方法:


/**     * 获取屏幕     */    private void screenshot() {        // 获取屏幕        View dView = getWindow().getDecorView();        dView.setDrawingCacheEnabled(true);        dView.buildDrawingCache();        Bitmap bmp = dView.getDrawingCache();        File file = PathGetUtils.getFile(this);        if (bmp != null) {            try {                Log.i("-->", "路径:" + file.getPath() + "  " + file.getAbsolutePath());                if (file.exists()) {                    file.delete();                }                FileOutputStream os = new FileOutputStream(file);                bmp.compress(Bitmap.CompressFormat.PNG, 100, os);                os.flush();                os.close();                String mUri = MediaStore.Images.Media.insertImage(getContentResolver(), file.getPath(), "ck", null);                if (mUri != null) {                    Toast.makeText(ShareCodeActivity.this, "截图成功", Toast.LENGTH_SHORT).show();                    // 最后通知图库更新                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);                    Uri uri = Uri.fromFile(new File(mUri));                    intent.setData(uri);                    sendBroadcast(intent);                } else {                    Toast.makeText(ShareCodeActivity.this, "截图失败", Toast.LENGTH_SHORT).show();                }            } catch (Exception e) {            }        }    }
其中获取的bitmap就是截屏图片.


第二种方法(这个只支持5.0的系统以上,截取整个视图):

调用方法,需要判断api版本

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){                    mMpMngr = (MediaProjectionManager) getApplicationContext().getSystemService(Context.MEDIA_PROJECTION_SERVICE);                    if(NoDoubleClick.isFastClick()){                        startIntent();                    }                }else {                    Toast.makeText(this, "您的手机系统暂不支持截屏功能", Toast.LENGTH_SHORT).show();                }


接下来是截图的方法

  @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        //3、通过onActivityResult()获取授权结果。        if (requestCode == REQUEST_MEDIA_PROJECTION && resultCode == RESULT_OK) {            MyApp.mResultCode = resultCode;            MyApp.mResultIntent = data;            MyApp.mMpmngr = mMpMngr;            newPrintScreen();        }    }    private void startIntent() {        if (MyApp.mResultIntent != null && MyApp.mResultCode != 0) {            //已授权            newPrintScreen();        } else{            //2、调用MediaProjectionManager.createScreenCaptureIntent()后,会弹出一个dialog询问用户是否授权应用捕捉屏幕            startActivityForResult(mMpMngr.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);//未授权        }    }    private void newPrintScreen(){        wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);        DisplayMetrics metric = new DisplayMetrics();        wm.getDefaultDisplay().getMetrics(metric);        windowWidth = metric.widthPixels;        windowHeight = metric.heightPixels;        screenDensity = metric.densityDpi;        mImageReader = ImageReader.newInstance(windowWidth, windowHeight, 0x1, 2);        Handler handler = new Handler();        handler.postDelayed(new Runnable() {            @Override            public void run() {                Log.e(TAG, "start startVirtual");                startVirtual();            }        }, 000);        handler.postDelayed(new Runnable() {            @Override            public void run() {                Log.e(TAG, "start startCapture");                startCapture();            }        }, 500);        handler.postDelayed(new Runnable() {            @Override            public void run() {                Log.e(TAG, "start stopVirtual");                stopVirtual();            }        }, 1000);    }    private void stopVirtual() {        if (mVirtualDisplay != null) {            mVirtualDisplay.release();            mVirtualDisplay = null;        }    }    private void startCapture() {        mImageName = System.currentTimeMillis() + ".png";        Log.e(TAG, "image name is : " + mImageName);        Image image = mImageReader.acquireLatestImage();        int width = image.getWidth();        int height = image.getHeight();        final Image.Plane[] planes = image.getPlanes();        final ByteBuffer buffer = planes[0].getBuffer();        int pixelStride = planes[0].getPixelStride();        int rowStride = planes[0].getRowStride();        int rowPadding = rowStride - pixelStride * width;        Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);        bitmap.copyPixelsFromBuffer(buffer);        bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);        image.close();        if (bitmap != null) {            Log.e(TAG, "bitmap  create success ");            try {                File fileFolder = new File(mImagePath);                if (!fileFolder.exists()) fileFolder.mkdirs();                File file = new File(mImagePath, mImageName);                if (!file.exists()) {                    Log.e(TAG, "file create success ");                    file.createNewFile();                }                FileOutputStream out = new FileOutputStream(file);                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);                out.flush();                out.close();                Log.e(TAG, "file save success ");                shareDialog(bitmap);                if (mMpj != null) {                    mMpj.stop();                    mMpj = null;                }            } catch (IOException e) {                Log.e(TAG, e.toString());                e.printStackTrace();            }        }    }    private void startVirtual() {        if (mMpj == null) mMpj = MyApp.mMpmngr.getMediaProjection(MyApp.mResultCode, MyApp.mResultIntent);        mVirtualDisplay = mMpj.createVirtualDisplay("capture_screen", windowWidth, windowHeight, screenDensity,                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null);    }


这种方法需要手动允许截屏的权限。