开发者

default camera to take a picture in android

How to use default camera to take a picture in android ?开发者_StackOverflow中文版


Uri imageUri;
final int TAKE_PICTURE = 115;

public void capturePhoto(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photoFile = new File(Environment.getExternalStorageDirectory(),  "Photo.png");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photoFile));
    imageUri = Uri.fromFile(photoFile);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImageUri = imageUri;
                //Do what ever you want
        }
    }
}


The intent which is used to open the camera is

 buttonCapturePhoto.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAPTURE_IMAGE);
        }
    });

The code which gives you the image after capturing is

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri uriImage;
    InputStream inputStream = null;
    if ( (requestCode == SELECT_IMAGE || requestCode == CAPTURE_IMAGE) && resultCode == Activity.RESULT_OK) {
        uriImage = data.getData();
        try {
            inputStream = getContentResolver().openInputStream(uriImage);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
            imageView.setImageBitmap(bitmap);
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setAdjustViewBounds(true);
    }
}


This is a simple example.Anyway this will return the image as a small bitmap.If you want to retrive the full-sized image ,is a bit more complicated.

ImageView  takePhotoView = (ImageView) findViewById(R.id.iwTakePicture);
Bitmap imageBitmap = null;
takePhotoView.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub              
                dispatchTakePictureIntent(0);
            }
        });

    private void dispatchTakePictureIntent(int actionCode) {
          Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);     
          startActivityForResult(takePictureIntent, actionCode);      
        }

    private void handleSmallCameraPhoto(Intent intent) {
        Bundle extras = intent.getExtras();
        this.imageBitmap = (Bitmap) extras.get("data");
        takePhotoView.setImageBitmap(imageBitmap);
    }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if(resultCode == RESULT_OK)
            handleSmallCameraPhoto(data);
   }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜