Android 安卓中的屏幕截图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3733988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:34:41  来源:igfitidea点击:

Screen Capture in android

androidscreenshot

提问by Sri Sri

I want to develop an application that will take screenshot of android screen..does any one know how to do it..? which is similar to koushik duttas screen shot..But with out using root..and does any one had koushik dutta screenshot application which is working..? is not working for me..please let me know..thanks in advance.

我想开发一个可以截取android屏幕截图的应用程序..有人知道怎么做吗..?这类似于 koushik duttas 屏幕截图..但是没有使用 root..并且有没有人有 koushik dutta 屏幕截图应用程序正在工作..?对我不起作用..请让我知道..提前致谢。

回答by Kristijan Dra?a

Let's say that you clicked on a button:

假设您单击了一个按钮:

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
       Bitmap bitmap = takeScreenshot();
       saveBitmap(bitmap);
   }
});

After that you need these two methods:

之后你需要这两种方法:

public Bitmap takeScreenshot() {
   View rootView = findViewById(android.R.id.content).getRootView();
   rootView.setDrawingCacheEnabled(true);
   return rootView.getDrawingCache();
}

 public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

回答by user741396

You can try something like this

你可以试试这样的

private RelativeLayout view;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    view = (RelativeLayout)findViewById(R.id.relativeView);

    View v1 = view.getRootView();

    v1.setDrawingCacheEnabled(true);
    Bitmap bm = v1.getDrawingCache();
}

回答by zpr

The method view.getDrawingCache() will first attempt to retrieve an image it has previously cached. This can cause issues if you want to guarantee your screenshot is up-to-date. For instance, if your user clicks your screenshot button, then changes the UI, then clicks it again, the second screenshot will be identical to the first unless you wipe the cache. I find the following method more convenient:

view.getDrawingCache() 方法将首先尝试检索它之前缓存的图像。如果您想保证屏幕截图是最新的,这可能会导致问题。例如,如果您的用户单击您的屏幕截图按钮,然后更改 UI,然后再次单击它,除非您擦除缓存,否则第二个屏幕截图将与第一个屏幕截图相同。我觉得下面的方法更方便:

public Bitmap takeScreenshot() {
  View rootView = findViewById(android.R.id.content).getRootView();
  Bitmap bitmap = Bitmap.createBitmap(rootView.getWidth(), rootView.getHeight(), Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  rootView.draw(canvas);
  return bitmap;
}

回答by Nicolas Raoul

I think it is impossible without root or the SDK, sorry.

我认为没有 root 或 SDK 是不可能的,抱歉。

I would love to be proven wrong.

我很想被证明是错误的。

Not an app, but if you have a USB cable, you can install the Android SDK on a PC and take screenshots from the PC with androidscreencast, without having to root your phone.

不是应用程序,但如果您有 USB 数据线,您可以在 PC 上安装 Android SDK,并使用androidscreencast从 PC 截取屏幕截图,而无需您的手机。