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

    xiaoxiao2021-03-26  23

    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; } } }
    转载请注明原文地址: https://ju.6miu.com/read-660224.html

    最新回复(0)