-2

I am building an app that requires a function to open the default camera application from device that installs my app; my question is how do I implement it?.

From what I've learned after Googling for awhile is how to open camera from within an app, but what i am trying to achieve is actually open the default camera that can use any feature the camera app has. Can you tell me how to implement the camera?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
A N Syafiq.
  • 143
  • 1
  • 3
  • 12

3 Answers3

2

You can open camera app with intent

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

Now get the image back once you are done with camera app

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
          Bitmap image = (Bitmap) data.getExtras().get("data");
          // do whatever you want with the image now
    }
}

Hope this would help.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
1

To use the inbuilt camera application in your android phone you need a few things First:

  • The device has to have a camera
  • Your App must have permission to take pictures
  • Your App must have permission to save the picture (if you intend to)

To take care of the above you will need to add the below code in your manifest file for permission to use camera for picture taking and if the main feature of your app required the device to have a camera

<uses-feature android:name="android.hardware.camera" android:required="true" />

You will also need to add in your manifest file permission to write / save the image if needed this permission also allows your app to have read permission (This example no need)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Finally remember you are asking Android to do work for you hence it will return a result to you which is the image for this will need to use startActivityForResult and pass the code or think of it as an Identifier ID that both your App and Android will use say 123 it can be any integer so long its not used in the activity for any other purpose than for this purpose taking a picture:

Member variable

static final int REQUEST_TAKE_PHOTO = 123;

private void takePicture() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
    }

Add this to a button or image button so a click will call takePicture method

Final Step is that remember there will be that image or result data that Android will return you will need to accept and as per above Identifier code REQUEST_TAKE_PHOTO = 123 / requestCode. There is a method that you will need to override

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            // Sample data cast to  thumbnail
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            // Do whatever you want with the thumbnail such as setting it to image view
            mImageView.setImageBitmap(imageBitmap);
        }
    }

Source: Android Take Photos

alkathirikhalid
  • 887
  • 12
  • 18
  • 1. AFAIK every android device has camera. 2. No permission or feature required to open camera app using intent. – Ketan Ahir Jan 18 '17 at 05:08
  • 1
    @KetanAhir Hi, I guess you missed the Source link attached at the bottom. 1. "Every android device has camera", not entirely true, Android is an OS and the OS is used in almost anything from Phones, Cars, TV, TV Boxes (Online Chanel's) to custom handheld order tablets e.t.c Lets Assume you did make a build for Android Phone and Tablets still we get back to "not entirely true" there are specific devices without cameras but has access to play store as explained above as simple Google Search "android phone no camera" will show you my point. – alkathirikhalid Jan 18 '17 at 06:01
  • @KetanAhir "No permission or feature required to open camera app using intent" true as Android will handle this, the original question was on "open the default camera" back to my first point checking for camera in the first place. It is best and recommended practice when building android apps "if the main feature of your app required the device to have a camera" you need to add this permission so as Google Play store will feature your app to devices that actually have a camera. Good old Android programming practices :) – alkathirikhalid Jan 18 '17 at 06:14
  • Agreed. But when we use intent to open default camera app we don't required permission. Again if camera app is installed in device that means device has camera. We just need handle ActivityNotFoundException when start camera app. So we do not need to take care of both of your points. – Ketan Ahir Jan 18 '17 at 06:33
0

Just send an Intent to the camera requesting a photo:

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

More details here: https://developer.android.com/training/camera/photobasics.html

DYZ
  • 55,249
  • 10
  • 64
  • 93
atwrk
  • 234
  • 1
  • 2
  • 9