Check whether the device has a camera - defaults to true on SDK versions < 7 - Android Camera

Android examples for Camera:Camera Feature

Description

Check whether the device has a camera - defaults to true on SDK versions < 7

Demo Code

/*/*from w  w w  .  j  av a 2  s . c  om*/
 *  Copyright (C) 2012 Simon Robinson
 * 
 *  This file is part of Com-Me.
 * 
 *  Com-Me is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation; either version 3 of the 
 *  License, or (at your option) any later version.
 *
 *  Com-Me is distributed in the hope that it will be useful, but WITHOUT 
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General 
 *  Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with Com-Me.
 *  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import android.annotation.TargetApi;
import android.content.pm.PackageManager;

import android.os.Build;

public class Main {
    /**
     * 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