Skip to content

Instantly share code, notes, and snippets.

@Nkzn
Created August 3, 2012 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nkzn/3247159 to your computer and use it in GitHub Desktop.
Save Nkzn/3247159 to your computer and use it in GitHub Desktop.
/*
* http://d.hatena.ne.jp/gabuchan/20101125/1290681748 の前半を参考にして作成
* (Xperia 2.1のdata.getData()の事例は例外と判断して切りました)
*/
Uri mCameraUri;
/**
* カメラ呼び出し部分
*/
void callCamera() {
// ファイル名を作成
String filename = System.currentTimeMillis() + ".jpg";
// 保存先のUriオブジェクトを作成
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, filename);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
mCameraUri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
// Intentでカメラ呼び出し
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraUri);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
}
/**
* 撮影結果受け取り部分
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_CAMERA:
// Uriがnullだったら諦める
if (mCameraUri == null) {
Toast.makeText(getActivity(), "撮影に失敗しました", Toast.LENGTH_LONG).show();
return;
}
useImage(mCameraUri); // 画像を使って何かする
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment