Search code examples
c#androidxamarinopenglopentk

Java.Lang.IllegalArgumentException: invalid Bitmap format Intermittently on startup


I am doing stress testing on a variety of android devices, using Firebase Testlab, and am getting an unusual exception occurring (on some cloud test devices) that results in a black screen when loading textures on startup. We have the main activity onCreate() generating MainPage, which then runs Init() which loads textures. It also dumps textures onPause and reloads onResume via calling Init() again. Here is the error:

Java.Lang.IllegalArgumentException: invalid Bitmap format
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at offset 12
at Java.Interop.JniEnvironment.StaticMethods.CallStaticVoidMethod(Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) at offset 110
at Java.Interop.JniPeerMembers.JniStaticMethods.InvokeVoidMethod(System.String encodedMember, Java.Interop.JniArgumentValue* parameters) at offset 24
at Android.Opengl.GLUtils.TexImage2D(System.Int32 target, System.Int32 level, Android.Graphics.Bitmap bitmap, System.Int32 border) at offset 112
at DoodleSmash.Droid.MainActivity.LoadTexture(System.String name) at offset 65
at DoodleSmash.MainPage.Init() at offset 689

It seems to think the bitmap is in the wrong format, but I have never seen this issue with other devices, I have run emulators and several physical devices locally. Here is the load texture code:

public int LoadTexture(string name)
        {
            var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MainActivity)).Assembly;
            System.IO.Stream stream = assembly.GetManifestResourceStream("DoodleSmash.Droid." + name);

            Android.Graphics.Bitmap bm = Android.Graphics.BitmapFactory.DecodeStream(stream);

            int tex;
            GL.GenTextures(1, out tex);
            GL.BindTexture(TextureTarget.Texture2D, tex);

            GLUtils.TexImage2D((int)TextureTarget.Texture2D, 0, bm, 0);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

            return tex;
        }

TexImage2D is the function that appears to be failing, I really don't know why. Any ideas? It is intermittent and occurs on startup, inconsistently. The images are all PNGs. Maybe I need to clear all texture memory first in the onCreate function? Any help would be greatly appreciated.


Solution

  • I figured it out, the bitmap format needs to be ARGB_8888 for older APIs and older phones, can be accomplished by changing the immutable bitmap via:

    Bitmap bmp_Copy = bmp_Base.copy(Bitmap.Config.ARGB_8888,true);
    

    then just recycle the old one.

    The answer came from: Convert immutable Bitmap file to mutable Bitmap