【问题标题】:Take Android Screenshot programmatically-whatever on screen-app may be in background-not rooted device以编程方式获取 Android 屏幕截图 - 屏幕上的任何应用程序都可能在后台 - 未植根设备
【发布时间】:2014-01-10 17:34:10
【问题描述】:

我想以编程方式截取 Android 设备的屏幕截图。我已经搜索了很多东西,但无论我找到什么,它都是说你的设备应该是 root 或者只有你可以截取你的应用程序的截图,即如果你的应用程序在后台,那么它不会截取主屏幕的截图。

简而言之,我想要屏幕截图,它可以为我提供用户可以看到的屏幕上的任何内容。我自己的应用程序可能在后台。

  1. 设备未植根
  2. 全屏(仅用户可见部分)
  3. 应用程序可能在后台(但仍应截取用户可见的任何内容,即可能是主屏幕)

【问题讨论】:

  • 此应用是否在监视您的用户?我认为您应该首先检查该任务是否合乎道德。

标签: android screenshot android-fullscreen


【解决方案1】:

要获取屏幕截图,您需要获取视图的根布局

getWindow().getDecorView();

将视图缓存到位图中

public static File getScreenShot(Context context,View parentLayout) 
    {
        parentLayout.setDrawingCacheEnabled(true);
        Bitmap cachedBitmap= parentLayout.getDrawingCache();
        Bitmap finalBitmap = cachedBitmap.copy(Bitmap.Config.RGB_565, true);
            return saveBitmap(context,finalBitmap);
    }

将位图保存在设备等上

private static File saveBitmap(Context context,Bitmap bitmap) 
    {
        File snapShot=null;
        try 
        {
            String cacheDirectory=getCacheDirectory(context);               
            snapShot=new File(cacheDirectory, "Screenshot.png");
            FileOutputStream out = new FileOutputStream(snapShot);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();
        } catch (Exception e) 
        {
               e.printStackTrace();
        }
        return snapShot;
    }

【讨论】:

  • 先生,我认为您的应用在后台时无法截屏。当我在后台运行时,我在上面的代码中得到 NullPointer 异常。
  • 当您尝试截屏时,前台是否有一些进度对话框或警告对话框???
  • 我尝试使用透明活动,以便我可以捕获背景,但它只显示黑屏。你能建议我下一个吗?如果我放置按钮或进度对话框,那么它只会捕获那部分。
  • 是的,上面的代码适用于它调用的视图或布局,尝试从你想要截图的活动中调用相同的代码
  • 伙计们,有人可以指导我在应用程序之外为非 root 设备获取屏幕截图吗?
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多