1

Is there any way I can support both deprecated and new API in the same method call for Android? I'm using the camera API which seems to be deprecated for the Lollipop version, so I tried to handle it like this:

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
    {
       //Before Lollipop, use the Camera API since it still supported.
    }
    else
    {
        //Use the CameraManager
        try
        {
            for (int i= 0; i < _camera.getCameraIdList().length; i++)
            {
                System.out.println("Camera= " + _camera.getCameraIdList()[i]);
            }
        }
        catch (CameraAccessException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

But this does however just give me the error Call requires API level 21 (current min is 15): android.hardware.camera2.CameraManager#getCameraIdList I tried SupressLint and TargetApi but that only made the device running an earlier (before Lollipop) Android version crash when creating an class instance of this type.

Thanks for any help!

Araw
  • 2,410
  • 3
  • 29
  • 57
  • 1
    "I tried SupressLint and TargetApi" -- `TargetApi` is the right answer. "that only made the device running an earlier (before Lollipop) Android version crash when creating an class instance of this type" -- we would need to see the full set of actual code and the full stack trace to help you fix it. Your general approach is the correct one, and it works for lots of developers in lots of cases. In this case, my guess is that the problem is wherever you are setting up `_camera` from that code listing. – CommonsWare Jun 08 '15 at 19:56
  • Can you include your logcat of the error message? Simply having code in a class does not crash any Android 2.0+ device - code actually needs to run for it to crash. – ianhanniballake Jun 08 '15 at 19:57
  • Aah, both of you are correct! @ianhanniballake your comment made me see my mistake. There was a CameraManager class instance which I didn't remove which (ofcourse) will make < Lollipop devices crash! – Araw Jun 08 '15 at 20:01

2 Answers2

2
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
               //handler lollipop and higher 
            } else {
               //earlier api calls
            }
rahul.ramanujam
  • 5,608
  • 7
  • 34
  • 56
1

Simply having code in a class does not crash any Android 2.0+ device - code actually needs to run for it to crash. Check to make sure all of your Lollipop specific code is wrapped in version checks.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443