屏幕截屏,dialog截屏

  • 截屏代码方法1
    private void screenShotAll() {
        View view = getWindow().getDecorView().getRootView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap temBitmap = view.getDrawingCache();
        File fileDir = new File(CAMERA_DIR);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        String path = CAMERA_DIR + "/all_screen.png";
        try {
            FileOutputStream foStream = new FileOutputStream(path);
            temBitmap.compress(Bitmap.CompressFormat.PNG, 100, foStream);
            foStream.flush();
            foStream.close();
            Toast.makeText(this,"截屏成功,请在相册目录下("+path+")查看",Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Log.i("Show", e.toString());
        }
    }

由于我的需求是 游客登录时自动截屏后台分配的用户名和密码到相册,正好我这里登录是一个dialog,然后dialog并没有显示,于是了解到可以单独截取dialog

  AlertDialog dialog;
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("等待1秒自动截屏dialog,请在相册目录下("+CAMERA_DIR+")查看");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        dialog = builder.create();
        dialog.show();
//        window = dialog.getWindow();
    }

先保存dialog,同时更改截屏方法第一行代码,通过dialog获取view

View view = dialog.getWindow().getDecorView();
  • 截屏代码方法2

    private void screenShotView(View view) {
        Bitmap temBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(temBitmap);
        view.draw(canvas);
        File fileDir = new File(CAMERA_DIR);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        String path = CAMERA_DIR + "/view_screen.png";
        try {
            FileOutputStream foStream = new FileOutputStream(path);
            temBitmap.compress(Bitmap.CompressFormat.PNG, 100, foStream);
            foStream.flush();
            foStream.close();
            Toast.makeText(this,"截屏成功,请在相册目录下("+path+")查看",Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Log.i("Show", e.toString());
        }
    }
    

    通过dialog获取到DecorView,截取指定view保存到相册

    screenShotView(dialog.getWindow().getDecorView());
    

    源码地址https://github.com/honaf/ScreenShot.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值