How to take a screenshot and share it programmatically

后端 未结 5 862
半阙折子戏
半阙折子戏 2020-12-02 05:40

I am making an application in Android in which I have to take screenshot of one of my activities and mail it as attachment.

I want to take screenshot of the current

5条回答
  •  情歌与酒
    2020-12-02 06:15

    This is how I captured the screen and shared it. Take a look if you are interested.

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

    And the method that saves the bitmap image to external storage:

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

    see more in : https://www.youtube.com/watch?v=LRCRNvzamwY&feature=youtu.be

提交回复
热议问题