안드로이드 개발 질문/답변
(글 수 45,052)
카메라 프로그램을 만들고 갤러리와 연동하는 프로그램을 만들고 있습니다
카메라 프리뷰 상태에서 캡쳐와 동시에 sdcard 에 저장하고 있는데 모든게 다 에러없이 잘 동작합니다
그런데 DDMS 로 sdcard 내부를 들여다보니 파일네임이 엉망인 것입니다
기본 카메라어플에선 너무나도 이쁘게 yyyy-mm-dd kk.kk.kk.jpg 로 저장되더군요
그래서 기본 어플 소스를 헤쳐보니 이것에 관한 소스를 찾았는데 너무나도 복잡하고 어렵네요
네이밍 하는 방법이 없을까요?
제 소스 입니다
FileOutputStream fos; try { String filename = "capture.jpg"; String pathForAppFiles = getFilesDir().getAbsolutePath(); pathForAppFiles = pathForAppFiles + "/" + filename; Log.d("Capture image filename:", pathForAppFiles); fos = openFileOutput(filename, MODE_WORLD_READABLE); fos.write(data); fos.close(); } catch(Exception e) { Log.e("Capture Error", "Capture Failed", e); } Log.v("Storage Start", "Storage Begin"); try { Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); String fileUrl = MediaStore.Images.Media.insertImage(getContentResolver(), bm, "imagename", ""); Log.d("fileUrl", fileUrl); } catch (Exception e) { Log.e("Still", "Error writing file", e); }
sdcard 저장에 접근할 수 있는 다른 방법이 있다면 가르쳐주세요~
2010.04.11 17:04:49
도움이 될지 모르겠지만 전 이렇게 했네요
private SimpleDateFormat timeStampFormat = new SimpleDateFormat("MM-dd-HH-mm-ss");
private String filename = timeStampFormat.format(new Date());
private String SDCARD_PATH = Environment.getExternalStorageDirectory().getAbsoluteFile()+"/eyeone";
File copyFile = new File(SDCARD_PATH+"/"+filename+".jpg");
//콜벡내부 메소드중.
public void onPictureTaken(byte[] data, Camera camera) {
try {
Log.e("-->", "onPictureTaken=" + data + " length = " + data.length);
//이미지 만들기
Bitmap input_image = BitmapFactory.decodeByteArray(data, 0, data.length);
// 이미지 축소
Bitmap resize = Bitmap.createScaledBitmap(input_image, 640, 480, true); ;
OutputStream out = null;
try {
copyFile.createNewFile();
out = new FileOutputStream(copyFile);
// 압축
if ( resize.compress(CompressFormat.JPEG, 75,out) ){
out.flush();
out.close();
Log.w("-->", "jpegsave-ok");
}else{
Log.w("-->", "jpegsave-false");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("-->", "File Save Errer");
}
}
private SimpleDateFormat timeStampFormat = new SimpleDateFormat("MM-dd-HH-mm-ss");
private String filename = timeStampFormat.format(new Date());
private String SDCARD_PATH = Environment.getExternalStorageDirectory().getAbsoluteFile()+"/eyeone";
File copyFile = new File(SDCARD_PATH+"/"+filename+".jpg");
//콜벡내부 메소드중.
public void onPictureTaken(byte[] data, Camera camera) {
try {
Log.e("-->", "onPictureTaken=" + data + " length = " + data.length);
//이미지 만들기
Bitmap input_image = BitmapFactory.decodeByteArray(data, 0, data.length);
// 이미지 축소
Bitmap resize = Bitmap.createScaledBitmap(input_image, 640, 480, true); ;
OutputStream out = null;
try {
copyFile.createNewFile();
out = new FileOutputStream(copyFile);
// 압축
if ( resize.compress(CompressFormat.JPEG, 75,out) ){
out.flush();
out.close();
Log.w("-->", "jpegsave-ok");
}else{
Log.w("-->", "jpegsave-false");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("-->", "File Save Errer");
}
}
이름을 날짜형식으로 바꾸실려면
SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
String strDate = formatter.format(today);
이런 식으로 사용하면 됩니다.