I am trying to create a bitmap screenshot from my opengl framebuffer to share through an intent with ACTION_SEND
The following is my code:
size_t size = w * h * 4;
uint8_t *pixels = new uint8_t[size];
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
jobject jbitmap = 0;
if(pixels != 0) {
JNIEnv * env = GetEnv(state_->activity->vm);
//create the jbyte array and fill in with the pixel data
int byte_array_length = w * h;
Log("byte array length = %d", byte_array_length);
jbyteArray byte_array = env->NewByteArray(byte_array_length);
env->SetByteArrayRegion(byte_array, 0, byte_array_length, (jbyte *)pixels);
//get the BitmapFactory class
jclass bitmap_factory_class = loadExternalClass("android/graphics/BitmapFactory");
jmethodID decode_byte_array_method = env->GetStaticMethodID(bitmap_factory_class,
"decodeByteArray", "([BII)Landroid/graphics/Bitmap;");
//get the bitmap itself
jbitmap = env->CallStaticObjectMethod(bitmap_factory_class, decode_byte_array_method,
byte_array, 0, byte_array_length);
env->DeleteLocalRef(byte_array);
}
if(jbitmap == 0) {
Log("Could not create image from framebuffer to share");
}
It looks like a load of crap, but in reality, all its doing is just loading the JNI BitmapFactory class, loading its decodeByteArray method, loading a JNI byteArray object with the pixel data and then calling the following alternative Java code
BitmapFactory.decodeByteArray(byte_array, 0, byte_array_length); //params: (array, offset, length)
However, the jbitmap object that is supposed to hold the image is always returning as 0
I have verified the pixels array read in by glReadPixels is valid and fine
What am I missing? Thanks a lot