Android: 屏幕截图

获取图片:

/**
 * 屏幕截屏
 * @param activity
 * @return
 */
public static Bitmap getShot(Activity activity) {
    // 获取windows中最顶层的view
    View view = activity.getWindow().getDecorView();
    view.buildDrawingCache();

    // 获取状态栏高度
    Rect rect = new Rect();
    view.getWindowVisibleDisplayFrame(rect);
    int statusBarHeights = rect.top;
    Display display = activity.getWindowManager().getDefaultDisplay();

    // 获取屏幕宽和高
    int widths = display.getWidth();
    int heights = display.getHeight();

    // 允许当前窗口保存缓存信息
    view.setDrawingCacheEnabled(true);

    // 去掉状态栏
    Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,
            statusBarHeights, widths, heights - statusBarHeights);

    // 销毁缓存信息
    view.destroyDrawingCache();

    return bmp;
}

保存图片:

public static void saveImage(Bitmap bmp, String dirName, String fileName) throws IOException {
    // 判断sd卡是否存在
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        String storagePath = Environment.getExternalStorageDirectory().toString();
        File dir = new File(storagePath, dirName);
        // 判断文件夹是否存在,不存在则创建
        if(!dir.exists()){
            dir.mkdir();
        }

        File file = new File(dir.getPath(), fileName);
        // 判断文件是否存在,不存在则创建
        if (!file.exists()) {
            file.createNewFile();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            if (fos != null) {
                // 第一参数是图片格式,第二个是图片质量,第三个是输出流
                bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
                // 用完关闭
                fos.flush();
                fos.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

参考:http://my.oschina.net/neil173352/blog/308353

你可能感兴趣的:(android)