android截屏 (非root,只限当前acitvity当中)

android截屏 (非root,只限当前acitvity当中)

秉承拿来就能用的原则!


【含有标题栏】

private void saveCurrentImage() {

    // 获取当前屏幕的大小
    int width = getWindow().getDecorView().getRootView().getWidth();
    int height = getWindow().getDecorView().getRootView().getHeight();
    // 生成相同大小的图片
    Bitmap temBitmap = Bitmap.createBitmap(width, height - 20, Config.ARGB_8888);
    // 找到当前页面的跟布局
    View view = getWindow().getDecorView().getRootView();
    // 设置缓存
    view.setDrawingCacheEnabled(true);

    // 防止因为【Android手动回收bitmap,引发Canvas: trying to use a recycled bitmap处理】而崩掉程序
    try {
        view.buildDrawingCache();
        // 从缓存中获取当前屏幕的图片
        temBitmap = view.getDrawingCache();
    } catch (Exception e) {
        // Toast.makeText(MainActivity.this, "SysErr", Toast.LENGTH_LONG).show();
    }

    // 输出到SD卡
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
    Date now = new Date();
    File skRoot = Environment.getExternalStorageDirectory();
    String picDir = skRoot.getPath() + Pictures";
    File destDir = new File(picDir);
    if (!destDir.exists()) {
        destDir.mkdirs();
    }
    String picPath = picDir + "/" + sdf.format(now) + ".png";
    File file = new File(picPath);

    try {
        if (!file.exists()) {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            temBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            Toast.makeText(MainActivity.this, "截图成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "截图已存在!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(MainActivity.this, "截图失败!", Toast.LENGTH_LONG).show();
    } finally {
        if (temBitmap != null && !temBitmap.isRecycled()) {
            // 清空缓冲区
            view.setDrawingCacheEnabled(false);
            // 回收并且置为null
            temBitmap.recycle();
            temBitmap = null;
        }
    }
}

——————————————————————————————————————————————————————————————

【没有标题栏】

private void saveCurrentImage() {

    // 获取当前屏幕的大小
    int width = getWindow().getDecorView().getRootView().getWidth();
    int height = getWindow().getDecorView().getRootView().getHeight();
    // 获取标题栏高度
    Rect frame = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    // 找到当前页面的跟布局
    View view = getWindow().getDecorView().getRootView();
    // 设置缓存
    view.setDrawingCacheEnabled(true);

    // 创建临时图片和最终图片对象【最好声明到方法外部】
    Bitmap temBitmap = null;
    Bitmap bitmap = null;

    // 防止因为【Android手动回收bitmap,引发Canvas: trying to use a recycled bitmap处理】而崩掉程序
    try {
        view.buildDrawingCache();
        // 从缓存中获取当前屏幕的图片
        temBitmap = view.getDrawingCache();
        bitmap = Bitmap.createBitmap(temBitmap, 0, statusBarHeight, width, height - statusBarHeight);
    } catch (Exception e) {
        // Toast.makeText(MainActivity.this, "SysErr",
        // Toast.LENGTH_LONG).show();
    }

    // 输出到SD卡
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
    Date now = new Date();
    File skRoot = Environment.getExternalStorageDirectory();
    String picDir = skRoot.getPath() + File.separator + "Pictures";
    File destDir = new File(picDir);
    if (!destDir.exists()) {
        destDir.mkdirs();
    }
    String picPath = picDir + File.separator + sdf.format(now) + ".png";
    File file = new File(picPath);

    FileOutputStream fos = null;
    try {
        if (!file.exists()) {
            file.createNewFile();
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            Toast.makeText(MainActivity.this, "截图成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "截图已存在!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(MainActivity.this, "截图失败!", Toast.LENGTH_LONG).show();
    } finally {
        try {
            if (null != fos) {
                fos.close();
            }
        } catch (Exception e) {
        }
        // 清空缓冲区
        view.setDrawingCacheEnabled(false);
        if (null != temBitmap && !temBitmap.isRecycled()) {
            // 回收并且置为null
            temBitmap.recycle();
            temBitmap = null;
        }
        if (null != bitmap && !bitmap.isRecycled()) {
            // 回收并且置为null
            bitmap.recycle();
            bitmap = null;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值