任何屏幕截图

3
我正在使用以下代码截取当前活动的屏幕截图:
View rootview = findViewById(android.R.id.content).getRootView();
rootview.setDrawingCacheEnabled(true);

并将图像保存在SD卡上。

但我想截取的不仅是这个应用程序,还有应用程序之外的内容。我做了一些研究发现,只有已 root 的设备才能实现这一点。但是否有办法在不 root 设备的情况下截取任何屏幕?


我相当肯定这不是,但并非百分之百确定。我知道很多公司现在将此功能作为内置功能包含其中,因为很多人想要它,但它受到未经root的限制。 - zgc7009
1
出于显而易见的隐私和安全原因,希望不会这样。 - CommonsWare
5个回答

3
尝试使用反射从PhoneWindowManager.java中调用takeScreenshot()方法。请参考此链接:这里
请参考这里中的代码。
针对PhoneWindowManager执行此操作。
TelephonyManager telephony = (TelephonyManager) 
  context.getSystemService(Context.TELEPHONY_SERVICE);  
  try {
      Log.v(TAG, "Get getTeleService...");
      Class c = Class.forName(telephony.getClass().getName());
      Method m = c.getDeclaredMethod("getITelephony");
      m.setAccessible(true);
      telephonyService = (ITelephony) m.invoke(telephony);
      telephonyService.silenceRinger();
      Log.v(TAG, "Answering Call now...");
      telephonyService.answerRingingCall();
      Log.v(TAG, "Call answered...");
      //telephonyService.endCall();
  } catch (Exception e) {
   e.printStackTrace();
   Log.e(TAG,
           "FATAL ERROR: could not connect to telephony subsystem");
   Log.e(TAG, "Exception object: " + e);
  }

Android L提供了API来捕获屏幕截图。请参考此链接:https://developer.android.com/reference/android/media/projection/package-summary.html


抱歉,我没听懂你的意思,请问你能否展示一下它如何帮助我? - Goofy
1
请参考此链接:http://jhshi.me/2014/04/02/get-package-usage-statistics-in-android/,可以获取Android应用程序包的使用统计信息。类似的方法也可以用于截屏。通常,如果有一些隐藏在框架中的方法,我们会使用反射来访问它们。 - rupesh jain

1
以下是允许将我的屏幕截图存储在SD卡上并稍后用于任何需要的代码:
// image naming and path  to include sd card  appending name you choose for file

String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

// create bitmap screen capture

Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {

fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

当需要访问时,请使用类似以下方式:

 Uri uri = Uri.fromFile(new File(mPath));

1

0

0

这取决于您的目的。如果您需要在分发到Playstore的应用程序中使用该功能,则没有运气。没有root权限,这是不可能的。

但是,对于测试或个人目的,adb会有所帮助。您只需向计算机的adb服务器发送一些数据包,即可检索设备的屏幕截图。

要做到这一点,您应该阅读adb客户端<->服务器协议framebuffer协议似乎非常适合您。

或者,如果您熟悉node.js,adbkit将为您节省时间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接