Sorry for asking stupid questions on the forum, I found the answer
myself: What I proposed above doesn't work.


I had to learn first a bit about reflection...


I adapted successfully (as it seems) the code from
http://developer.android.com/resources/articles/backward-compatibility.html
to this problem. Before it was unclear to me how the example code
shown in the articel knows in context of what object it get's
executed. But now its obvious. What works for me is:


// Call from my Method:
...
List<Size> supportedSizes = Reflect.getSupportedPictureSizes
(parameters);
...


// Adapted Reflect Class:
public class Reflect {
        private static Method Parameters_getSupportedPictureSizes;
        private static Method Parameters_getSupportedPreviewSizes;

        static {
                initCompatibility();
        };

        private static void initCompatibility() {
                try {
                        Parameters_getSupportedPictureSizes =
Camera.Parameters.class.getMethod(
                                        "getSupportedPictureSizes", new Class[] 
{ } );
                        /* success, this is a newer device */
                } catch (NoSuchMethodException nsme) {
                        /* failure, must be older device */
                }
                try {
                        Parameters_getSupportedPreviewSizes =
Camera.Parameters.class.getMethod(
                                        "getSupportedPreviewSizes", new Class[] 
{ } );
                        /* success, this is a newer device */
                } catch (NoSuchMethodException nsme) {
                        /* failure, must be older device */
                }
        }

        @SuppressWarnings("unchecked")
        public static List<Size> getSupportedPictureSizes(Camera.Parameters
p) {
                try {
                        if (Parameters_getSupportedPictureSizes != null) {
                                return (List<Size>) 
Parameters_getSupportedPictureSizes.invoke
(p);
                        } else {
                                return null;
                        }
                } catch (InvocationTargetException ite) {
                        /* unpack original exception when possible */
                        Throwable cause = ite.getCause();
                        if (cause instanceof RuntimeException) {
                                throw (RuntimeException) cause;
                        } else if (cause instanceof Error) {
                                throw (Error) cause;
                        } else {
                                throw new RuntimeException(ite);
                        }
                } catch (IllegalAccessException ie) {
                        System.err.println("unexpected " + ie);
                        return null;
                }
        }

        @SuppressWarnings("unchecked")
        public static List<Size> getSupportedPreviewSizes(Camera.Parameters
p) {
                try {
                        if (Parameters_getSupportedPreviewSizes != null) {
                                return (List<Size>) 
Parameters_getSupportedPreviewSizes.invoke(p);
                        } else {
                                return null;
                        }
                } catch (InvocationTargetException ite) {
                        /* unpack original exception when possible */
                        Throwable cause = ite.getCause();
                        if (cause instanceof RuntimeException) {
                                throw (RuntimeException) cause;
                        } else if (cause instanceof Error) {
                                throw (Error) cause;
                        } else {
                                /* unexpected checked exception; wrap and 
re-throw */
                                throw new RuntimeException(ite);
                        }
                } catch (IllegalAccessException ie) {
                        return null;
                }
        }
}








On 13 Jan., 23:23, Dani <[email protected]> wrote:
> Thanks a lot four your answer!
>
> I see that this function is supported since API level 5.
>
> When I try to call it from the emulator running level 5 and higher I
> always receive a null-list back when asking for
> getSupportedPictureSizes. This is a bit inconvenient but I can work
> around that. I keep my fingers crossed and hope it will work on a
> Nexus :-)
>
> In order to support also lower API levels, I changed my code to this:
>
> ...
> try { //check if method is available
>   Camera.Parameters.class.getMethod("getSupportedPictureSizes", new
> Class[] { });} catch (NoSuchMethodException nsme) {
>
>     return someDefaultSize;}
>
> List<Size> supportedSizes  = parameters.getSupportedPictureSizes();
> ...
> /* Evaluate best fitting size and return it */
>
> I am wondering if this is good practice. I also read this artical
> about the compatibility 
> topic:http://developer.android.com/resources/articles/backward-compatibilit...
> But I didn't manage to apply the there proposed solution "Using
> reflection" with a cached method to this problem....
>
> On 12 Jan., 07:35, Wu-cheng Li (李務誠) <[email protected]> wrote:
>
>
>
> > On Mon, Jan 11, 2010 at 8:51 PM, Dani <[email protected]> wrote:
> > > Hello
>
> > > I keep getting error messages when running my photo applications on a
> > > Nexus One with api version 7 when the program tries to set the camera
> > > parameters:
>
> > > ...
> > > Camera.Parameters parameters = camera.getParameters();
>
> > Please call getSupportedPreviewSizes to get valid preview sizes. 533x320 is
> > invalid on Nenus 
> > One.http://developer.android.com/reference/android/hardware/Camera.Parame...()
>
> > > parameters.setPreviewSize(w, h); // data set: w:533, h:320
>
> > camera.setParameters(parameters);> ...
>
> > > ...
> > > Camera.Parameters parameters = camera.getParameters();
>
> > Please call getSupportedPictureSizes to get valid picture 
> > sizes.http://developer.android.com/reference/android/hardware/Camera.Parame...()
>
> > > parameters. setPictureSize(w, h); // data set w:1280, h:768
>
> > camera.setParameters(parameters)
>
> > > ...
>
> > > The prorgram works fine on various other mobile phones such as moto
> > > droid or HTC Hero.
>
> > > Any idea what it could be? I would appreciate some help!
>
> > > cheers
> > > Dani
>
> > > ----------------------------------------------
> > > The stack trace I get is:
> > > ----------------------------------------------
> > > java.lang.RuntimeException: setParameters failed
> > >       at android.hardware.Camera.native_setParameters(Native Method)
> > >       at android.hardware.Camera.setParameters(Camera.java:619)
> > >       at dk.android.bcFiler.PreviewSurface.surfaceChanged
> > > (PreviewSurface.java:149)
> > >       at android.view.SurfaceView.updateWindow(SurfaceView.java:460)
> > >       at android.view.SurfaceView.dispatchDraw(SurfaceView.java:287)
> > >       at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
> > >       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
> > >       at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
> > >       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
> > >       at android.view.View.draw(View.java:6538)
> > >       at android.widget.FrameLayout.draw(FrameLayout.java:352)
> > >       at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
> > >       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
> > >       at android.view.View.draw(View.java:6538)
> > >       at android.widget.FrameLayout.draw(FrameLayout.java:352)
> > >       at com.android.internal.policy.impl.PhoneWindow$DecorView.draw
> > > (PhoneWindow.java:1830)
> > >       at android.view.ViewRoot.draw(ViewRoot.java:1349)
> > >       at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
> > >       at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
> > >       at android.os.Handler.dispatchMessage(Handler.java:99)
> > >       at android.os.Looper.loop(Looper.java:123)
> > >       at android.app.ActivityThread.main(ActivityThread.java:4363)
> > >       at java.lang.reflect.Method.invokeNative(Native Method)
> > >       at java.lang.reflect.Method.invoke(Method.java:521)
> > >       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
> > > (ZygoteInit.java:860)
> > >       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
> > >       at dalvik.system.NativeStart.main(Native Method)
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Android Developers" group.
> > > To post to this group, send email to [email protected]
> > > To unsubscribe from this group, send email to
> > > [email protected]<android-developers%2Bunsubs
> > >  [email protected]>
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-developers?hl=en
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to