2

有一个简单的教程可以用我遵循的 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() 崩溃。

有任何想法吗?

谢谢格哈德

4

2 回答 2

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%

于 2012-05-16T13:53:15.350 回答
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);
于 2018-05-25T12:00:21.807 回答