0

I am trying to use the camera class to take a picture programatically in an Android application. The target API is the newest (N as of writing this) but the minimum API is 15.

Prior to API level 21, this was the standard:

android.hardware.Camera camera = Camera.open();
camera.takePicture(foos, ro, da);

Now, it is deprecated, and they recommend you use this one

CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
manager.openCamera(foos, ro, da);
//A few more steps, but the key one here is the getSystemService call as it gives the warning

But the problem is that If I use the second one in my app, which has a minimum API of 15, it warns me that it will not work. (Screenshot Here), which seems like it will cause problems if used on devices with an API of < 21

Is there a 3rd, other class I should be using in this scenario to use the camera functionality? Am I missing something? Or should I really just run the if check for their build and if < 21, run the deprecated code and if >= 21, use the new code?

Thanks!

-PGMac

Edit 1: To clarify further, whenever Android deprecates something, I have seen them make the new, non-deprecated code, backwards compatible so that you don't need to use it anymore. A couple of examples would be: getColor, setBackgroundDrawable, and more. I know that here in this answer they indicate what I already mentioned, which is I could use an if check and compare, but my concern is that the deprecated class will eventually be unusable and cause the code to break. That's really the essence of this question, Is that fear justified? Should I just use an if check? Or is there a better way to do it by using a different class entirely?

Community
  • 1
  • 1
PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • 1
    You can put a condition to check the sdk level of the phone your app is running on. Accordingly you can call the methods if less than 21 or greater than 21. – Arjun Sep 01 '16 at 02:55
  • Try your code by checking version code name: `if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ //API 21 ... } else { ..}` – pRaNaY Sep 01 '16 at 03:14
  • Thanks guys, but I as I mentioned in my post, I am aware of just running an if else check on the api level, I was asking about other options. – PGMacDesign Sep 01 '16 at 04:50

1 Answers1

0

If you want to target old devices below 21 then you already using a right Camera class even it's deprecated. If you will use the new class recommended by the android, you will not be able to target the devices below 21 API level so if you want to target maximum devices in your app you should use the old camera class.

Awais Ahmad
  • 427
  • 3
  • 17
  • Thanks, but I as I mentioned in my post, I am aware of just running an if else check on the api level, I was asking about other options. – PGMacDesign Sep 01 '16 at 04:50
  • There is not any other class provided by android developers other than these 2. And don't be fear to use the old deprecated class and it will not cause to break your code.. I have already used it and it just works fine on all the devices – Awais Ahmad Sep 01 '16 at 05:15