Check if device Has Camera - Android android.hardware

Android examples for android.hardware:Camera State

Description

Check if device Has Camera

Demo Code

import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Build;
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main{

    /**/*from   w w w  . ja  v  a  2s . c om*/
     * Check whether the device has a camera - <b>defaults to true</b> on SDK versions < 7
     * 
     * @param packageManager
     * @return
     */
    // @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) is for PackageManager.FEATURE_CAMERA_ANY
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static boolean deviceHasCamera(PackageManager packageManager) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ECLAIR_MR1) {
            return true;
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
            return packageManager
                    .hasSystemFeature(PackageManager.FEATURE_CAMERA);
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return packageManager
                    .hasSystemFeature(PackageManager.FEATURE_CAMERA)
                    || packageManager
                            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
        } else {
            return packageManager
                    .hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
        }
    }

}

Related Tutorials