2

I am looking to use Android's Camera.open() method on two separate API levels. First is API level 7, which is 2.1 and higher and the second is 2.3.3 & 2.3.4 which are API level 9.

On API level 7 and 8 the Camera.open method does not take any arguments. On API level 9 and above, the camera takes an integer argument that supplies it the cameraId to use.

How can I target both API levels in the same code? Something similar to this pseudo code:

Camera lCamera;
if (Platform.APILevel < 7){
  lCamera.open();
}else { 
  lCamera.open(0);
}
divibisan
  • 11,659
  • 11
  • 40
  • 58
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105

3 Answers3

7

We often do reflection detection. Something like:

    public Camera getCamera() {
        try {
            Method method = Camera.class.getMethod("open", Integer.TYPE);
            return (Camera) method.invoke(null, 0);
        } catch (Exception e) {
                // Yes, I really want to handle all exceptions here!!!!
                Log.d(TAG,"Error when trying to invoke Camera.open(int), reverting to open()",e);
                return Camera.open();
        }
    }
Jarek Potiuk
  • 19,317
  • 2
  • 60
  • 61
  • :) Cool. It works very well in case the reflection is not run too often (Camera retrieval is ok) - because reflection is slow. There is also another interesting way, but somehow I found it not very convincing ... You could compile (or download) latest and greatest android-all-classes jar file to your eclipse project (but keeping it outside of lib directory). And wrap it in Exception like above... This would compile in eclipse very well, only in runtime you'd get exception. This add extra complexity if you want to make ant build though. ... – Jarek Potiuk Jun 28 '11 at 22:08
  • Just a follow up... What about import statements that require classes not found in previous versions of SDK's? – Kevin Parker Jun 29 '11 at 05:24
  • That's the .jar approach I wrote about above. – Jarek Potiuk Jun 29 '11 at 16:34
  • would i have to set targetSdk to 9 in order to do this, or could i keep it at 7? – Mohamed Hafez Sep 19 '13 at 00:06
2

Generally speaking, you just target the lowest API level you can get away with. Anything built for API level 7 will work for API level 8, 9, 10, etc. In this case, just call lCamera.open(); and it will choose the first back-facing camera found. If you want to use a front camera, you have to use the higher level API level 9.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • Not true - For 3.0 onwards you may need to call `invalidateOptionsMenu()`. On all lower API versions this method does not exist so you need to use reflection to call it if it exists. – Graeme Aug 10 '11 at 09:32
2

You can get the API level by reading the final static integer android.os.Build.VERSION.SDK_INT.

In your manifest, make sure that you set android:minSdkVersion AND android:targetSdkVersion.

Computerish
  • 9,590
  • 7
  • 38
  • 49
  • This also helps by just always targeting the highest level API needed then wrapping around areas that aren't supported at the highest required API level. The only way to check what needs to be changed to get around lower API levels seems to be to change your project's Target Build continuously. – Kevin Parker Jun 28 '11 at 23:52
  • `Sorry, I thought you were looking for how to implement the `Platform.APILevel`. – Computerish Jun 29 '11 at 00:29