zoukankan      html  css  js  c++  java
  • Android 截屏并写入SD卡中

    -----------截屏方法

    View Code
    private Bitmap shot() {  
        View views = getWindow().getDecorView();
        views.buildDrawingCache();
    
        // 获取状态栏高度
        Rect frames = new Rect();
        views.getWindowVisibleDisplayFrame(frames);
        int statusBarHeights = frames.top;
        Display display = getWindowManager().getDefaultDisplay();
        int widths = display.getWidth();
        int heights = display.getHeight();
        //第一种方式        
        views.layout(0, statusBarHeights,widths, heights - statusBarHeights);
        views.setDrawingCacheEnabled(true);//允许当前窗口保存缓存信息 ,两种方式都需要加上
          Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache());
        //第二种方式        
        // 1、source 位图  2、X x坐标的第一个像素  3、Y y坐标的第一个像素  4、宽度的像素在每一行  5、高度的行数
        //Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache(), 0, statusBarHeights,widths, heights - statusBarHeights);
        return bmp;  
    }

    ---------保存到SD卡方法

    View Code
                try {
                    String status = Environment.getExternalStorageState();
                    // 判斷SD卡是否存在
                    if (status.equals(Environment.MEDIA_MOUNTED)) {
                        File destDir = new File("文件夹名");
                        
                        if (!destDir.exists()) {
                            // 创建文件夾
                            destDir.mkdirs();
                        }
                        File file = new File("图片名");
                        // 判断文件夾是否存在
                        if (file.exists()) {
                            String pic_path ="文件夹名" +"图片名"+".png";
                            FileOutputStream out = new FileOutputStream(pic_path);
                            shot().compress(Bitmap.CompressFormat.PNG,100, out);
                            out.flush();
                            out.close();
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

    -------把Bitmap转为Drawable 放进imageView中

    View Code
            //Bitmap-->Drawable  
                    BitmapDrawable bd=new BitmapDrawable(shot());  
                    imageView.setBackgroundDrawable(bd);  
                    imageView.setImageBitmap(shot()); 
  • 相关阅读:
    写Log日志的方法 减少插件引用
    操作文件常用的方法
    Git常用命令
    JS
    js
    BUG++
    mysql点滴记录 二 (MySql经典练习题)
    mysql点滴记录 一 (创建表结构 & 构建测试数据)
    TCPDF
    Docker-命令
  • 原文地址:https://www.cnblogs.com/androidsj/p/3069380.html
Copyright © 2011-2022 走看看