有一个简单的教程可以用我遵循的 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();
}
}
我在活动中调用它。现在的问题是,当camera Activity出现时,我可以拍多张照片,但我只想拍一张照片,然后返回调用的Activity。在我离开相机活动(通过按“后退”按钮)之前不会调用 onActivityResult。然后 data.getData() 崩溃。
有任何想法吗?
谢谢格哈德