Search code examples
androidandroid-intentcameraandroid-4.2-jelly-bean

Is there any way to do a safe intent to camera for all level apis?


From Froyo to Ice Cream Sandwich this is the way:

Intent intent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "test.jpg");
tempUriCameraDeviceNotSuported = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
tempUriCameraDeviceNotSuported);
startActivityForResult(intent, 0);

But for Jelly Bean it doesn't work, you must do something like this:

Intent takePicture = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);

I'm not sure if this bug happens only in jelly bean, or it also depends on the hardware model. Google doesn't tell a word about this issue (why? why?). Someone knows where can I find a list with all devices and their right camera implementation? Someone knows the best way to do this?


Solution

  • You can always check for the OS version:

        if (android.os.Build.VERSION.SDK_INT >= 17) {
            Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takePicture, 0);
        } else {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File file = new File(Environment.getExternalStorageDirectory(),
                    "test.jpg");
            tempUriCameraDeviceNotSuported = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    tempUriCameraDeviceNotSuported);
            startActivityForResult(intent, 0);
        }