Skip to content

Instantly share code, notes, and snippets.

@masarugen
Created August 14, 2012 09:10
Show Gist options
  • Save masarugen/3347723 to your computer and use it in GitHub Desktop.
Save masarugen/3347723 to your computer and use it in GitHub Desktop.
/**
* 画像のディレクトリパスを取得する
*
* @return
*/
private String getDirPath() {
String dirPath = "";
File photoDir = null;
File extStorageDir = Environment.getExternalStorageDirectory();
if (extStorageDir.canWrite()) {
photoDir = new File(extStorageDir.getPath() + "/" + getPackageName());
}
if (photoDir != null) {
if (!photoDir.exists()) {
photoDir.mkdirs();
}
if (photoDir.canWrite()) {
dirPath = photoDir.getPath();
}
}
return dirPath;
}
/**
* 画像のUriを取得する
*
* @return
*/
private Uri getPhotoUri() {
long currentTimeMillis = System.currentTimeMillis();
Date today = new Date(currentTimeMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
String title = dateFormat.format(today);
String dirPath = getDirPath();
String fileName = "samplecameraintent_" + title + ".jpg";
String path = dirPath + "/" + fileName;
File file = new File(path);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, title);
values.put(Images.Media.DISPLAY_NAME, fileName);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.DATA, path);
values.put(Images.Media.DATE_TAKEN, currentTimeMillis);
if (file.exists()) {
values.put(Images.Media.SIZE, file.length());
}
Uri uri = getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
return uri;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment