-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera
77 lines (66 loc) · 2.56 KB
/
camera
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.example.camera;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btnCamera, btnAlbum;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 디자인 정의
btnCamera = (Button) findViewById(R.id.btnPhoto);
btnAlbum = (Button) findViewById(R.id.btnAlbum);
imageView = (ImageView) findViewById(R.id.imageView);
btnCamera.setOnClickListener(this);
btnAlbum.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btnPhoto) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
} else if (view.getId() == R.id.btnAlbum) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
} else if (requestCode == 1) {
if(resultCode == RESULT_OK) {
try {
InputStream in = getContentResolver().openInputStream(data.getData());
Bitmap img = BitmapFactory.decodeStream(in);
in.close();
imageView.setImageBitmap(img);
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
else if(resultCode == RESULT_CANCELED) {
Toast.makeText(this, "사진 선택 취소", Toast.LENGTH_LONG).show();
}
}
}
}