2

This is my code. I want to select an image from either camera or gallery. For this i have to check the camera support feature of a device.

  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();

        }

I have tried a lot but no success.

2 Answers2

4
import android.hardware.Camera;`

Use Camera.getNumberOfCameras();. If it returns value greater than 0, it means your device has a camera.

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
1

Use this to show gallery to pick image from:

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

Use this to open camera (checks if device has camera or not):

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();
        }
    }

NOTE: Make sure you declare permission for Camera in the manifest as:

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

For more details, check this out.

Mangesh
  • 5,491
  • 5
  • 48
  • 71
  • your method is also works for me but i wan't the simple one. only 1 or 2 line code. means like true or false. –  Mar 15 '16 at 07:26
  • Yes. So you only need the if-else from `showCamera()`. – Mangesh Mar 15 '16 at 08:33