zoukankan      html  css  js  c++  java
  • 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) {
                }
            }
        }
    
    今天多一点积累,明天少一分烦恼
  • 相关阅读:
    C盘的可用空间忽大忽小
    安装软件时不能指定软件的安装目录
    MySQL安装排坑
    Butterfly主题目录生成不了问题
    apache+php安装配置的各种问题
    环境变量配置不成功
    http模块
    Node.js
    Spring Boot入门
    代码优化笔记
  • 原文地址:https://www.cnblogs.com/galibujianbusana/p/6128848.html
Copyright © 2011-2022 走看看