java 在android中拍照的默认相机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5945517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:35:58  来源:igfitidea点击:

default camera to take a picture in android

javaandroidcamerapng

提问by Sawan Modi

How to use default camera to take a picture in android ?

如何使用默认相机在android中拍照?

采纳答案by Sawan Modi

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
        }
    }
}

回答by Vineet

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);
    }
}

回答by Roman Marius

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);
   }