微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 如何拍摄屏幕快照不仅在Android中的应用程序与代码

我开发了应用程序,需要截图.
但它只是应用程序的快照.我想从应用程序中拍摄快照.
我已经研究了答案,但我还没有找到答案.
这是我的代码
View view = getwindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
saveImagetoAppFolder(bitmap);

saveImagetoAppFolder是将图像保存到应用程序文件夹的功能.
这不是问题.
有没有拍摄屏幕的快照?

解决方法

要拍摄设备屏幕的屏幕截图,只有有root用户
调用screencap二进制如:
Process sh = Runtime.getRuntime().exec("su",null,null);
OutputStream  os = sh.getoutputStream();
os.write(("/system/bin/screencap -p " + Environment.getExternalStorageDirectory()+ "/img.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor()

并将该文件加载到位图中,使用

public static Bitmap decodeSampledBitmapFromFile(String path,int reqWidth,int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path,options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path,options);
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options,int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfheight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfheight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

原文地址:https://www.jb51.cc/android/123226.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐