我正在尝试通过反射在JNI(C ++)中创建一个Android Bitmap对象(Java)。下面的代码工作正常,直到Android L版本,但在Android M上没有。在Android M中,mNativeBitmap的名称被修改为mNativePtr,这被考虑在内。即使在此更改代码崩溃之后。
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);
如果我删除了位图 - > getPixels()的memcpy,那么代码不会崩溃。所以我们怀疑由于memcpy调用导致一些内存损坏。如果有人能解决这个问题,请帮我解决。