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 87
47 내부 저장소 접근 함수 API 와 실제 저장소 경로 황제낙엽 2018.08.21 85
46 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-06 file 황제낙엽 2018.08.19 991
45 안드로이드 파일 객체 생성자 황제낙엽 2018.08.19 90
44 파일 입출력(내장 메모리, 외장메모리) 황제낙엽 2018.08.19 720
43 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-05 황제낙엽 2018.08.19 132
42 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-04 file 황제낙엽 2018.08.12 1711
» 뷰 캡처하여 이미지 파일로 저장하기(SD카드로 화면 캡처)-03 file 황제낙엽 2018.08.12 150
40 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-02 황제낙엽 2018.08.12 133
39 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-01 황제낙엽 2018.08.12 86
38 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-00 황제낙엽 2018.08.12 109
37 [HttpURLConnection] 서버와 세션 유지 황제낙엽 2018.08.12 58
36 [HttpURLConnection] 세션 관리 황제낙엽 2018.08.12 27
35 이미지 크기 변경(Image resize) file 황제낙엽 2018.08.09 108
34 Emulator: audio: Failed to create voice `adc' 황제낙엽 2018.08.06 1007
33 install_failed_invalid_apk file 황제낙엽 2018.08.06 49
32 Image to byte Array (바로 사용가능한 JPEG 파일) 황제낙엽 2018.07.24 612
31 안드로이드 스튜디오(Android Studio) 최적화 file 황제낙엽 2018.02.07 433
30 Android Studio for beginners, Part 4: Advanced tools and plugins (2) file 황제낙엽 2018.02.02 14
29 Android Studio for beginners, Part 4: Advanced tools and plugins (1) file 황제낙엽 2018.02.02 26