有一个简单的教程可以用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()崩溃。
有什么想法吗?
谢谢 格哈德
答案 0 :(得分:1)
嗯......这对我来说可以捕捉到图像:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
activity.startActivityForResult(intent, AbstractActivity.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
当用户拍照时,会询问用户是否想要接受此图片,然后又回到我的活动中...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
handleAvatarUpload(data); // which uses Uri selectedImage = data.getData();
}
}
}
我没有收到任何有关此代码的崩溃报告,并且它已经出现了几个月。我们的用户群与我们类别中的其他应用程序一致,其中Android 2.3.3约占用户的71%,2.2为16.35%,2.1为5.7%
答案 1 :(得分:0)
我知道这是一个老问题,但我遇到了同样的问题。
对我而言,它是通过不提供URI而是URI路径myUri.getPath()
以下应该做你想做的事:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri.getPath());
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);