Search code examples
javac++candroid-ndkjava-native-interface

JNI NewIntArray() can't create big array


I use JNI calls to load PNG files with Android class Bitmap using this guide http://lol.zoy.org/blog/2011/3/2/load-pngs-using-android-ndk.
And this works flawlessly until i try to get pixel data from Bitmap. I just can't create a jint array which will fit image data (512*256), app just crash on this line. I do some test and it works without crash only with jint arrays which size <= 85000 (on my HTC Desire). I think it's some out of memory error but i get no relevant error in logcat and i try to only create a jint array without any other code it crash too.

#include <jni.h>    

int load_image_png(const char* path, GLuint* width, GLuint* height, void** image_data){
//Skip part what works fine - get bitmap width and height
//width=512, height=256

 jintArray array = g_env->NewIntArray(width*height);//FAIL OVERHERE

 jint* pixels = g_env->GetIntArrayElements(array, 0);
 *image_data = pixels;

 //closing a bitmap work fine too

 return 0;

}

P.S. It will be awesome if someone can give alternative way of loading png from Java code (without pnglib and native functions like in http://androgeek.info/?p=275)


Solution

  • Exceptions don't occur automagically in JNI. You have to write code to trigger them. i.e. it stores the error somewhere and you have to add code to say where you want the exception to be triggered.

    http://java.sun.com/docs/books/jni/html/exceptions.html


    From http://www.google.co.uk/search?q=java+png+library

    http://code.google.com/p/javapng/

    http://code.google.com/p/pngj/

    I don't know which is better.