Android : how to check if device has camera

In Android, you can use PackageManager , hasSystemFeature() method to check if a device has camera, gps or other features.

See full example of using PackageManager in an activity class.


package com.mkyong.android;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

public class FlashLightActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.main);

		Context context = this;
		PackageManager packageManager = context.getPackageManager();

		// if device support camera?
		if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
			//yes
			Log.i("camera", "This device has camera!");
		}else{
			//no
			Log.i("camera", "This device has no camera!");
		}

		
	}
}
Camera flashlight example
You may interest on this example – How to turn on/off camera LED/flashlight in Android.

Reference

  1. Android PackageManager Javadoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Joe
Guest
Joe

does not work, there is a bug

mussharapp
Guest
mussharapp

According to the docs here
http://developer.android.com/reference/android/content/pm/PackageManager.html#FEATURE_CAMERA, FEATURE_CAMERA pertains to the back camera. So if you are using a
device with only the front facing camera(like the first gen Nexus 7) then the code above won’t work. I think you should also check FEATURE_CAMERA_FRONT.

Nayyi
Guest
Nayyi

why

Log.i("Camera","This has a camera");

doesn’t work?

Agustin Ernesto
Guest
Agustin Ernesto

Thank you so much for this tutorials !; they are like a starting point for me… although I am asked to develop ADF Apps, I think this would be a good way to earn some more bucks… now I am under the account of a bank but soon under the account of a credit card… so I’ll be proposing my idea in some more months… blessings

Mike
Guest
Mike

Thanks for the informative blog! Any idea how to mock/shadow the PackageManager for Android?

trackback
Android Tutorial

[…] – How to center button on screenHow to turn on/off camera LED/flashlight in AndroidAndroid : how to check if device has cameraHow to send SMS message in AndroidHow to send Email in AndroidAndroid : The connection to adb is […]