In this post I’ll show how you can take a screenshot of your current Activity and save the resulting image on /sdcard.

The idea behind taking a screenshot actually is pretty simple: what we need to do is to get a reference of the root view and  generate a bitmap copy of this view.

 

screenshot.jpg

 

 

Considering that we want to take the screenshot when a button is clicked, the code will look like this:

 

 

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

   @Override

   public void onClick(View v) {

       Bitmap bitmap = takeScreenshot();

       saveBitmap(bitmap);

   }

});

 

First of all we should retrieve the topmost view in the current view hierarchy, then enable the drawing cache, and after that call getDrawingCache().

Calling getDrawingCache(); will return the bitmap representing the view or null if cache is disabled, that’s why setDrawingCacheEnabled(true); should be set to true prior invoking  getDrawingCache().

 

 

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

    }

}

 

Since the image is saved on external storage, the WRITE_EXTERNAL_STORAGE permission should be added AndroidManifest to file:

 

 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /

번호 제목 글쓴이 날짜 조회 수
128 PC를 위한 최고의 안드로이드 에뮬레이터 황제낙엽 2023.01.18 3137
127 [Jetpack Compose UI / 상추님] GDG Jetpack compose 코드랩 수료 후기 (feat. 굿즈) 황제낙엽 2022.12.24 2372
126 [Jetpack Compose UI / kyj1991719] compose - Compose 이해 황제낙엽 2022.12.24 4025
125 [Jetpack Compose UI / 꾸준] Compose 가 뭔데...? 황제낙엽 2022.12.24 4036
124 [Jetpack Compose UI / IIVIKI] Compose 학습과 분석 황제낙엽 2022.12.24 4050
123 [Jetpack Compose UI] 스터디 시작하기 황제낙엽 2022.12.24 4001
122 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 바텀네비게이션 뷰, 네비게이션 AAC file 황제낙엽 2022.12.22 3882
121 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 뷰바인딩 file 황제낙엽 2022.12.22 3852
120 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 라이브데이터 뷰모델 file 황제낙엽 2022.12.21 3828
119 [대왕놀] ImageButton in Compose (IconButton) 황제낙엽 2022.12.20 4107
118 [대왕놀] compose instrumentation test tutorial 황제낙엽 2022.12.20 3986
117 [대왕놀] Switch in Compose 황제낙엽 2022.12.20 4107
116 [대왕놀] EditText in Compose (TextField) 황제낙엽 2022.12.20 4022
115 [대왕놀] margin in Compose 황제낙엽 2022.12.20 4105
114 [대왕놀] modifier in compose 황제낙엽 2022.12.20 4182
113 [대왕놀] Button in Compose file 황제낙엽 2022.12.20 4225
112 [대왕놀] TextView in Compose (Text) 황제낙엽 2022.12.20 4083
111 [대왕놀] ImageView in Compose (Image) 황제낙엽 2022.12.20 4019
110 [대왕놀] LinearLayout in Compose (Row, Column) 황제낙엽 2022.12.20 4114
109 [대왕놀] Introduction to Jetpack compose 황제낙엽 2022.12.20 3965