The default camera for shooting in android

How to use the default camera for shooting in android?

+6
source share
3 answers
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 } } } 
+7
source

The purpose 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 that gives you the image after capture,

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

+7
source

This is a simple example. In any case, this will return the image as a small bitmap. If you want to get a full-sized image, it's a little 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
source

Source: https://habr.com/ru/post/887684/


All Articles