空结果来自Intent,它在Android中调用Camera

时间:2015-02-08 08:36:03

标签: android android-intent

当相机从startActivityForResult()的意图开始并且获得空值(这意味着没有显示图片)时,我将获得空值。 相机拍照但活动中没有显示。 Plz帮助我。以下是拍摄Image Activity的代码。

public class TakeImage extends ActionBarActivity {
private LinearLayout camera;
private LinearLayout gallery;
ImageView targetImage;
Uri fileUri = null;
Button process;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_take_image);
    camera=(LinearLayout)findViewById(R.id.button);
    gallery=(LinearLayout)findViewById(R.id.button1);
    targetImage=(ImageView)findViewById(R.id.setImage);
    process=(Button)findViewById(R.id.process);
    camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, RESULT_OK);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    InputStream stream = null;
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==1&&resultCode == RESULT_OK && data != null){
        Uri targetUri = data.getData();
        Bitmap bitmap;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
            targetImage.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

阅读本Documentation

首先在清单中定义以下内容。

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

然后执行以下操作:

// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

希望它有助于