【问题标题】:How to check is there camera installed in the device or not?如何检查设备中是否安装了摄像头?
【发布时间】:2016-06-30 11:56:01
【问题描述】:

这是我的代码。我想从相机或图库中选择图像。 为此,我必须检查设备的相机支持功能。

  public void selectImage() {
            final String[] items = new String[]{"Camera", "Gallery"};
            final Integer[] icons = new Integer[]{R.drawable.ic_camera, R.drawable.ic_gallery};
            ListAdapter adapter = new ArrayAdapterWithIcon(getActivity(), items, icons);
            new AlertDialog.Builder(getActivity()).setTitle("Select Picture")
                    .setAdapter(adapter, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            if (items[item].equals("Camera")) {
                                if (//here i want to check is there mobile exists camera or not) {
                                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    startActivityForResult(intent, REQUEST_CAMERA);
                                } 
                            } else if (items[item].equals("Gallery")) {

                                    Intent intent = new Intent();
                                    intent.setType("image/*");
                                    intent.setAction(Intent.ACTION_GET_CONTENT);
                                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

                            }
                        }
                    }).show();

        }

我尝试了很多,但没有成功。

【问题讨论】:

标签: android


【解决方案1】:
import android.hardware.Camera;`

使用Camera.getNumberOfCameras();。如果它返回值大于0,则表示您的设备有摄像头。

【讨论】:

    【解决方案2】:

    使用它来显示图库以从中选择图像:

    private void showGallery() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, 1234);
    }
    

    使用它打开相机(检查设备是否有相机):

    private void showCamera() {
            // get the package manager and check if device has camera
            PackageManager pm = getPackageManager();
            if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
                // ensure that there is some activity to handle camera intents
                if (takePictureIntent.resolveActivity(pm) != null) {
                    startActivityForResult(takePictureIntent, 5678);
                }
            } else {
                Snackbar.make(mView, "your device dont have camera", Snackbar.LENGTH_LONG).show();
            }
        }
    

    注意:确保在清单中声明相机的权限为:

    <manifest ... >
        <uses-feature android:name="android.hardware.camera"
                      android:required="true" />
        ...
    </manifest>
    

    更多详情,请查看this

    【讨论】:

    • 你的方法也适用于我,但我不想要简单的方法。只有 1 或 2 行代码。意思是真假。
    • 是的。所以你只需要showCamera()的if-else。
    猜你喜欢
    • 2010-12-28
    • 2013-08-12
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 2020-03-05
    相关资源
    最近更新 更多