1

I am trying to create a Android Bitmap object(Java) in JNI(C++) through reflection. Below code was working fine until Android L version but not on Android M. In Android M, name of mNativeBitmap is modified to mNativePtr which is taken into consideration. Even after this change code is crashing.

 bitmapClazz = env->FindClass("android/graphics/Bitmap");
 createBitmapMethod = env->GetStaticMethodID(bitmapClazz, "createBitmap", 
        "(IILandroid/graphics/Bitmap$Config;)" 
        "Landroid/graphics/Bitmap;");

#ifdef AND_VER_6_0
    jfieldID nativeBitmap = env->GetFieldID(bitmapClazz, "mNativePtr", "J");
#elif AND_VER_5_0
    jfieldID nativeBitmap = env->GetFieldID(bitmapClazz, "mNativeBitmap", "J");
#endif

jclass configClazz = env->FindClass("android/graphics/Bitmap$Config");

 jmethodID createConfigMethod = env->GetStaticMethodID(configClazz, "nativeToConfig","(I)Landroid/graphics/Bitmap$Config;");
 jfieldID RB565FieldId = env->GetStaticFieldID(configClazz, "RGB_565", "Landroid/graphics/Bitmap$Config;");

jobject RGB565Config = env->GetStaticObjectField(configClazz, RB565FieldId);            
jobject jBitmap = env->CallStaticObjectMethod( bitmapClazz,  
            createBitmapMethod, 
            width, 
            height, 
            RGB565Config);

#if defined AND_VER_5_0 || defined AND_VER_6_0
            SkBitmap *bitmap =
                (SkBitmap *) env->GetLongField(jBitmap, nativeBitmap);
#else
            SkBitmap *bitmap =
                (SkBitmap *) env->GetIntField(jBitmap, nativeBitmap);
#endif

 memcpy((uint8_t*)bitmap->getPixels(),
                        (uint8_t*)thumbnail_PixelArray, height*width*2);

If I remove the memcpy of bitmap->getPixels() then the code is not crashing. So we suspect some memory corruption due to memcpy call. If anyone has solution to this problem please help me resolve it.

Cherma
  • 11
  • 2

0 Answers0