启动相机意图时未调用onActivityResult?

时间:2013-10-17 00:46:15

标签: android android-activity android-camera

我有一项活动,点按按钮相机活动。有时onActivityResult被调用,有时不被调用。即使在重新启动设备后,也不会调用onActivityResult,也不会重新启动当前活动。解决这种奇怪的行为?

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        String imageUri = data.toURI();
        Uri uri = Uri.parse(imageUri);
        try {
            mBitmap = Media.getBitmap(getContentResolver(), uri);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // if result is ok returns the bitmap
        // mBitmap = (Bitmap) data.getExtras().get("data");
        mImageView.setImageBitmap(mBitmap);
        new Thread(postTheImage).start();
    } else {
        Toast.makeText(getApplicationContext(), "Error during capturing the image", Toast.LENGTH_SHORT).show();
    }
}

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.capture_image_button) {
            // Open the camera to capture the image
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    }

1 个答案:

答案 0 :(得分:0)

您没有为相机活动提供所需的信息。您需要为其提供文件URI。

请参阅以下

onActivityResult returned from a camera, Intent null