sitelink1  
sitelink2  
sitelink3  

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" /

번호 제목 글쓴이 날짜 조회 수
48 [안드로이드 웹뷰] 파일 시스템으로 부터 HTML 로딩 황제낙엽 2018.08.21 196
47 내부 저장소 접근 함수 API 와 실제 저장소 경로 황제낙엽 2018.08.21 184
46 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-06 file 황제낙엽 2018.08.19 1121
45 안드로이드 파일 객체 생성자 황제낙엽 2018.08.19 194
44 파일 입출력(내장 메모리, 외장메모리) 황제낙엽 2018.08.19 841
43 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-05 황제낙엽 2018.08.19 236
42 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-04 file 황제낙엽 2018.08.12 1821
» 뷰 캡처하여 이미지 파일로 저장하기(SD카드로 화면 캡처)-03 file 황제낙엽 2018.08.12 285
40 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-02 황제낙엽 2018.08.12 241
39 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-01 황제낙엽 2018.08.12 210
38 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-00 황제낙엽 2018.08.12 218
37 [HttpURLConnection] 서버와 세션 유지 황제낙엽 2018.08.12 170
36 [HttpURLConnection] 세션 관리 황제낙엽 2018.08.12 168
35 이미지 크기 변경(Image resize) file 황제낙엽 2018.08.09 320
34 Emulator: audio: Failed to create voice `adc' 황제낙엽 2018.08.06 1867
33 install_failed_invalid_apk file 황제낙엽 2018.08.06 183
32 Image to byte Array (바로 사용가능한 JPEG 파일) 황제낙엽 2018.07.24 738
31 안드로이드 스튜디오(Android Studio) 최적화 file 황제낙엽 2018.02.07 556
30 Android Studio for beginners, Part 4: Advanced tools and plugins (2) file 황제낙엽 2018.02.02 149
29 Android Studio for beginners, Part 4: Advanced tools and plugins (1) file 황제낙엽 2018.02.02 154