Showing posts with label Camera Device. Show all posts
Showing posts with label Camera Device. Show all posts

Wednesday, May 21, 2014

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.

  1. package com.mkyong.android;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.pm.PackageManager;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. public class FlashLightActivity extends Activity {
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. //setContentView(R.layout.main);
  12. Context context = this;
  13. PackageManager packageManager = context.getPackageManager();
  14. // if device support camera?
  15. if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
  16. //yes
  17. Log.i("camera", "This device has camera!");
  18. }else{
  19. //no
  20. Log.i("camera", "This device has no camera!");
  21. }
  22. }
  23. }

Camera flashlight example

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