Android手机截屏方法

本文介绍了一种在Android应用中实现截图功能的方法。通过获取屏幕视图并将其转换为位图,然后将位图保存到设备的外部存储中指定的位置。文章提供了两种截图实现方式,并附带了状态栏截取的相关代码。
  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 private void screenshot()
    {
        // 获取屏幕
        View dView = getWindow().getDecorView();
        dView.setDrawingCacheEnabled(true);
        dView.buildDrawingCache();
        Bitmap bmp = dView.getDrawingCache();
        if (bmp != null)
        {
            try {
                // 获取内置SD卡路径
                String sdCardPath = Environment.getExternalStorageDirectory().getPath();
                // 图片文件路径
                String filePath = sdCardPath + File.separator + "screenshot.png";

                File file = new File(filePath);
                FileOutputStream os = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
                os.flush();
                os.close();
            } catch (Exception e) {
            }
        }
    }
 //  这里是截取状态栏
@Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.activity_main, menu); return true; }

 

 

 

 private void screenshot1(String Strname)
    {
        /**
         * Strname--如  2.png  需要带后缀
         * */
        // 获取屏幕
        View dView = getWindow().getDecorView();
        dView.setDrawingCacheEnabled(true);
        dView.buildDrawingCache();
        Bitmap bmp = dView.getDrawingCache();
        if (bmp != null)
        {
            try {
                // 获取内置SD卡路径
                String sdCardPath = Environment.getExternalStorageDirectory().getPath();
                // 图片文件路径

                File filedir = new File(sdCardPath + File.separator + "AA");  // 这里的AA为创建的AA文件夹,在根目录下
                if (!filedir.exists()) {
                    filedir.mkdirs();
                }
                File saveFile = new File(filedir, Strname);  //  aaaa.txt保存到AA文件夹下
                FileOutputStream outStream1 = new FileOutputStream(saveFile);
                System.out.println(outStream1.toString());
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                outStream1.write(byteArray);
                outStream1.close();
                Toast.makeText(Zhuye_Activity.this,"截图保存成功",Toast.LENGTH_SHORT).show();
                dView.setDrawingCacheEnabled(false);  // 这里不设置false,那么下次截图还是上次的图片
            } catch (Exception e) {
            }
        }
    }

 

转载于:https://www.cnblogs.com/galibujianbusana/p/6128848.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值