sitelink1 http://ghj1001020.tistory.com/m/6 
sitelink2  
sitelink3  

버튼클릭 -> 화면캡쳐 -> 캡쳐한 이미지를 갤러리에 추가

 

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    //캡쳐버튼클릭
    public void mOnCaptureClick(View v){
        //전체화면
        View rootView = getWindow().getDecorView();
 
        File screenShot = ScreenShot(rootView);
        if(screenShot!=null){
            //갤러리에 추가
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(screenShot)));
        }
    }
 
    //화면 캡쳐하기
    public File ScreenShot(View view){
        view.setDrawingCacheEnabled(true);  //화면에 뿌릴때 캐시를 사용하게 한다
 
        Bitmap screenBitmap = view.getDrawingCache();   //캐시를 비트맵으로 변환
 
        String filename = "screenshot.png";
        File file = new File(Environment.getExternalStorageDirectory()+"/Pictures", filename);  //Pictures폴더 screenshot.png 파일
        FileOutputStream os = null;
        try{
            os = new FileOutputStream(file);
            screenBitmap.compress(Bitmap.CompressFormat.PNG, 90, os);   //비트맵을 PNG파일로 변환
            os.close();
        }catch (IOException e){
            e.printStackTrace();
            return null;
        }
 
        view.setDrawingCacheEnabled(false);
        return file;
    }
cs

 

 

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:textSize="24dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="화면을 캡쳐합니다." />
 
    <Button
        android:text="캡쳐하기"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="mOnCaptureClick"/>
 
    <ImageView
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
 


cs

 

 

AndroidManifest.xml

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

저장소에 파일 쓰기 권한

 

 

결과

캡쳐하기 버튼을 누르면 화면을 캡쳐하여 갤러리에 추가한다.

 

img1.daumcdn.net.png  img1.daumcdn.net1.png

 

 

 

번호 제목 글쓴이 날짜 조회 수
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
» 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-04 file 황제낙엽 2018.08.12 1711
41 뷰 캡처하여 이미지 파일로 저장하기(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