Android reverse code compatibility

I am developing an application that uses

android.hardware.Camera.parameters.getSupportedPictureSizes ()

This is only available in SDK version 8, and I would like to be compatible with SDK 4, so I did this:

if (Build.VERSION.SDK_INT> = 8) {...}

But on the emulator, he enters that tries to check the link to this function, and he fails:

02-02 11: 20: 10.930: ERROR / dalvikvm (1841): Could not find the android.hardware.Camera $ Parameters.getSupportedPictureSizes method referenced by the com.test.demo.CameraCustom.takeAPicture method

Any idea on how to solve this backward compatibility issue?

I tried using inkocation with this piece of code inside surfaceChanged. Obviously, the code works directly without calling:

try{ windowmanager_defaultdisplay_Rotation = getWindowManager().getDefaultDisplay().getClass().getMethod("getRotation"); Log.v(MainMenu.TAG, "getRotation exist"); }catch(Exception e){ Log.v(MainMenu.TAG, "getRotation dont exist"); } try{ windowmanager_defaultdisplay_Rotation.invoke(null, null); Log.v(MainMenu.TAG, "getRotation invoking ok, rotation "); }catch(Exception e){ Log.v(MainMenu.TAG, "exception invoking getRotation "+e.toString()); } 

I get "getRotation exist", but then "exception invoke getRotation java.lang.NullPointerException.

Any idea?

+7
source share
2 answers

You cannot load code containing getSupportedPictureSizes() calls at API level 7 or earlier. Therefore, you need to make a Build based decision before downloading code that contains a version-specific statement.

Your options include:

  • Disable a menu selection, button, or anything else that will result in activity that uses getSupportedPictureSizes() based on the API level
  • Use conditional class loading or similar methods to load the appropriate implementation based on the API level, where the "suitable implementation" uses getSupportedPictureSizes() only at API level 8 or higher

An example of the latter technique can be seen in this example project , where I support cameras with direct visibility at API level 9, but can still work on older versions of Android.

+5
source

Well, the answer provided by Commonsware is correct, especially if you look at the great sample project that it provided. Also, zegnus was on the right track when he pointed to http://developer.android.com/resources/articles/backward-compatibility.html

The key to this, although incomprehensible from another answer, is that you need to compile an API that supports the functions you need. Otherwise, you will get errors. In the Commonsware example, feedback cameras are first supported in API level 9, and this is what you must specify in your project before it can be compiled. You can then use the other methods described above to check if the OS running the application supports the actual classes and / or methods that you are trying to use. If your application runs on an older version of the OS, calls will throw an exception that you can catch and take appropriate action for the older OS.

For completeness, here is the code that I used for compatibility with API 7, although I compiled with API 8, which includes ThumbnailUtils.

 import com.Flashum.util.WrapThumbnailUtils; public static Bitmap createVideoThumbnail(String filePath, int kind) { try { WrapThumbnailUtils.checkAvailable(); // will cause exception if ThumbnailUtils not supported return WrapThumbnailUtils.createVideoThumbnail(filePath, kind); } catch (Exception e) { return null; } } package com.Flashum.util; import android.graphics.Bitmap; import android.media.ThumbnailUtils; // To be compatible with Android 2.1 need to create // wrapper class for WrapThumbnailUtils. public class WrapThumbnailUtils { /* class initialization fails when this throws an exception */ static { try { Class.forName("android.media.ThumbnailUtils"); } catch (Exception ex) { throw new RuntimeException(ex); } } /* calling here forces class initialization */ public static void checkAvailable() {} public static Bitmap createVideoThumbnail(String filePath, int kind) { return ThumbnailUtils.createVideoThumbnail(filePath, kind); } } 
+3
source

All Articles