简体   繁体   English

Android中的屏幕截图

[英]Screen Capture in android

I want to develop an application that will take screenshot of android screen..does any one know how to do it..? 我想开发一个应用程序,它将截取android屏幕截图..任何人都知道该怎么做..? 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..? 这类似于koushik duttas屏幕截图。但是没有使用root ..并且任何人都有koushik dutta截图应用程序正在工作..? is not working for me..please let me know..thanks in advance. 是不是为我工作..请让我知道..谢谢你提前。

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);
    }
}

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();
}

The method view.getDrawingCache() will first attempt to retrieve an image it has previously cached. 方法view.getDrawingCache()将首先尝试检索先前缓存的图像。 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. 例如,如果您的用户单击屏幕截图按钮,然后更改UI,然后再次单击它,第二个屏幕截图将与第一个屏幕截图相同,除非您擦除缓存。 I find the following method more convenient: 我发现以下方法更方便:

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;
}

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上截取屏幕截图,而不必使用手机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM