2

I'm seeing a few different things online so I wanted to make sure I was doing it correctly:

To check if a phone has a camera:

PackageManager pm = context.getPackageManager();

if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    //proceed
}

But I also see from http://developer.android.com/training/camera/photobasics.html

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
PackageManager pm = context.getPackageManager();

if (takePictureIntent.resolveActivity(pm) != null) {
    //proceed
 }

I know the first one is "correct" for verifying that the camera exists or not, but what is the second one doing exactly? Is it a good idea to actually check for both conditions before proceeding, or does it suffice to just check one or the other?

According to http://developer.android.com/reference/android/content/pm/PackageManager.html#resolveActivity%28android.content.Intent,%20int%29 it says that resolveActivity "Determine the best action to perform for a given Intent" but I don't really know what that entails. Is this another way to "check for the camera"?

KaliMa
  • 1,970
  • 6
  • 26
  • 51

2 Answers2

3

but what is the second one doing exactly?

It is determining if there is an app, installed on the device, that offers an ACTION_IMAGE_CAPTURE activity that you can start. There might be no such app, or there might be an app but the current user may not have rights to it.

Is it a good idea to actually check for both conditions before proceeding, or does it suffice to just check one or the other?

Either use resolveActivity()/queryIntentActivities() to detect in advance if there is an app for the implicit Intent that you are going to invoke, or catch the ActivityNotFoundException that will be raised if you call startActivity()/startActivityForResult() and there is no matching activity. This is true for any implicit Intent.

Technically, checking whether there is a hardware camera is more the responsibility of the app using the camera APIs. However, you might check that yourself, particularly if you want to disable UI options (e.g., action bar items) when the user cannot take a picture. In that case, only enable the UI option if:

  • there is a camera, and
  • there is an ACTION_IMAGE_CAPTURE activity
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • For the sake of just allowing the app to take pictures using a third party camera: Is it correct to say that `resolveActivity()` is effectively already taking care of the camera-check, because it'll be looking for something (like a camera which offers an `ACTION_IMAGE_CAPTURE` activity) to use when you run `startActivityForResult()`? – KaliMa Apr 11 '16 at 17:47
  • Also how does one designate an Activity as being an acceptable `ACTION_IMAGE_CAPTURE` activity to be called? (edit: Maybe this is the job of the intent-filter in the Manifest?) What if there are multiple such activities? (I'm guessing it'll prompt the user to choose among them?) – KaliMa Apr 11 '16 at 17:50
  • @KaliMa: "Is it correct to say that resolveActivity() is effectively already taking care of the camera-check" -- no. There is nothing stopping a camera-less device (e.g., Android TV box) from having some `ACTION_IMAGE_CAPTURE` app installed (e.g., installed by the user by accident). "Maybe this is the job of the intent-filter in the Manifest?" -- yes. "I'm guessing it'll prompt the user to choose among them?" -- also yes. – CommonsWare Apr 11 '16 at 18:01
0

the second one is checking if there is a camera app on the device that accepts the camera intent. There is nothing wrong with checking to make sure if there is an app that can handle the camera intent.

While I guess its possible for the device to not have a camera app when it has a camera, it is extremely unlikely.

tyczj
  • 71,600
  • 54
  • 194
  • 296