This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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