오늘은 안드로이드 그림판 이미지를 캡쳐하여 저장하는 예제소스입니다.

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
34
public class MainActivity extends Activity implements 
View.OnClickListener {
    Button mCaptureBtn;
    TouchView mTouchView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mCaptureBtn = (Button)findViewById(R.id.btn_capture);
        mCaptureBtn.setOnClickListener(this);
 
        mTouchView = (TouchView)findViewById(R.id.touchview);
    }
 
 
    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_capture)
        {
            mTouchView.buildDrawingCache();
            Bitmap captureView = mTouchView.getDrawingCache();
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/capture.png");
                captureView.compress(Bitmap.CompressFormat.PNG, 
100, fos);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
 
            Toast.makeText(getApplicationContext(), 
"Image Captured!", Toast.LENGTH_LONG).show();
        }
    }
}
cs


위 소스에서 핵심은 onClick 메소드 안입니다. 실제로 View들은 DrawingCache정보를 가지고 있습니다. 

이를 이용하여 Bitmap으로 가져와서 저장하는 방법입니다.

아래 예제 소스도 올려드리겠습니다.

안드로이드 kitkat 4.4 버전, 안드로이드 스튜디오에서 작업한 내용입니다. 필요하신 분들 참고하십시오.


* 분할 압축하였습니다. 참고하세요.

ViewCaptureTest.vol1.egg

ViewCaptureTest.vol2.egg

ViewCaptureTest.vol3.egg


블로그 이미지

미네르바98

안드로이드와 영화 리뷰, 생활정보에 관한 내용을 기재합니다.

,