안드로이드 이야기

안드로이드 - 카메라 촬영 후 저장

mapagilove 2012. 10. 17. 22:53
728x90
반응형

안드로이드 - 카메라 촬영 후 저장

원본 출처:http://www.yeory.com/category/Android

카메라 및 앨범은 MediaScannerConnectionClient 인터페이스를 구현해야만 사용가능하다.

 
 
 
 
 
 
 
 
 
 
 
 
 
public class ProfileEditActivity extends Activity implements MediaScannerConnectionClient{
private final String targetPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/DCIM/Camera/";
@Override
public void onMediaScannerConnected() {
msc.scanFile(targetPath, null);
}
@Override
public void onScanCompleted(String path, Uri uri) {
msc.disconnect();
}
}
요렇게 - 그리고 메서드를 Override 해주면 사용 준비 끝.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
private SimpleDateFormat sdf;
private File targetFile;
private void doTakePhotoAction(){
sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("return-data", true);
File targetFolder = new File(targetPath);
if(!targetFolder.exists()){
targetFolder.mkdirs();
try {
targetFolder.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 사진 촬영 후 저장될 경로
targetFile = new File(targetFolder, sdf.format(Calendar.getInstance().getTime())+".jpg");
photoUri = Uri.fromFile(targetFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, CommonResource.TAKE_PICTURE);
}


 

doTakePhotoAction()을 호출하게 되면 카메라가 호출되어 캡처가 가능하다.
CommonResource.TAKE_PICTURE는 코드값으로 카메라 캡처가 종료되면 해당 코드값으로 촬영인지 앨범인지 구분하기 위해 넣어준 것.


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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
private BitmapFactory.Options options = new BitmapFactory.Options();
private Uri photoUri;
private String uploadProfileImagePath = null;
private ImageView profileImage = null;
private MediaScannerConnection msc = null;
@Overide
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap profileBitmap = null;
Bitmap resized = null;
if(resultCode != RESULT_OK)
return;
// 이미지 품질 옵션
options.inJustDecodeBounds = true;
switch(requestCode){
// 갤러리에서 사진 가지고 올때
case CommonResource.SHOW_ALBUM:
photoUri = data.getData();
// 갤러리에서 선택했을때 실제 경로 가져오기 위해 커서
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( photoUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
photoUri = Uri.parse(cursor.getString(column_index));
profileBitmap = BitmapFactory.decodeFile(photoUri.getPath(), options);
// 비트맵 사이즈 조절
options = commonUtil.getBitmapSize(options, ImageDownloader.NORMAL_IMAGE);
profileBitmap = BitmapFactory.decodeFile(photoUri.getPath(), options);
uploadProfileImagePath = photoUri.getPath();
break;
case CommonResource.TAKE_PICTURE:
try {
uploadProfileImagePath = photoUri.getPath();
profileBitmap = BitmapFactory.decodeFile(photoUri.getPath(), options);
ContentValues values = new ContentValues();
values.put(Images.Media.MIME_TYPE, "bbom/jpeg");
values.put(MediaStore.Images.ImageColumns.DATA, photoUri.getPath());
photoUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
// 비트맵 사이즈 조절
options = commonUtil.getBitmapSize(options, ImageDownloader.NORMAL_IMAGE);
profileBitmap = BitmapFactory.decodeFile(uploadProfileImagePath, options);
try {
File targetFolder = new File(targetPath);
if(!targetFolder.exists()){
targetFolder.mkdirs();
targetFolder.createNewFile();
}
// 갤러리에 저장할때는 이미지 손실 없이 저장.
if(targetFile == null)
targetFile = new File(targetFolder, "BBOM_"+sdf.format(Calendar.getInstance().getTime())+".jpg");
FileOutputStream out = new FileOutputStream(targetFile);
// 찍은 비트맵을 실제 파일로 저장
profileBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally{
targetFile = null;
// 스캐너 연결해 주어야 갤러리에 실시간으로 나타남.
if(msc == null)
msc = new MediaScannerConnection(ProfileEditActivity.this, this);
msc.connect();
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
profileImage.setImageBitmap(profileBitmap);
profileImage.setScaleType(ImageView.ScaleType.FIT_XY);
System.gc();
}
찍고 끝나는게 아니라 찍고 파일로 저장하고 미디어스캐너에 연결해주어야 함.
728x90
반응형