有一個簡單的教程,可以用我遵循的android拍攝照片(http://developer.android.com/guide/topics/media/camera.html)。拍攝單張照片
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
protected void onTakePicture()
{
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// create a file to save the image
File file = Environment.getExternalStorageDirectory();
String path = file.getAbsolutePath() + "/Android/data/com.myapplication/files";
File dir = new File(path);
if (!dir.isDirectory())
dir.mkdirs();
File outFile = new File(path + File.separator + "img.jpg");
Uri fileUri = Uri.fromFile(outFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode==CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
{
// Image captured and saved to fileUri specified in the Intent
Uri fn = data.getData();
Toast.makeText(this, "Image saved to:\n" + fn.toString(), Toast.LENGTH_LONG).show();
}
}
我從一個活動中調用它。現在的問題是,當相機活動出現時,我可以拍攝多張照片,但我只想拍攝一張照片,然後返回到通話活動。 onActivityResult將不會被調用,直到我離開相機活動(通過按「後退」按鈕)。然後data.getData()崩潰。
任何想法?
謝謝 格哈德
你試試這個,而不是意向=新意圖(媒體商店。 INTENT_ACTION_STILL_IMAGE_CAMERA); –
我試過了,但沒有區別... – Gerhard