JNI NewIntArray() 无法创建大数组

发布于 2025-01-03 09:12:19 字数 1008 浏览 5 评论 0 原文

我使用 JNI 调用使用 Android 类 Bitmap 加载 PNG 文件,使用本指南 http://lol.zoy.org/blog/2011/3/2/load-pngs-using-android-ndk
在我尝试获取像素数据之前,这一切都完美无缺来自位图。我只是无法创建适合图像数据(512*256)的 jint 数组,应用程序只是在这条线上崩溃。我做了一些测试,它仅适用于大小 <= 85000 的 jint 数组(在我的 HTC Desire 上),不会崩溃。 我认为这是一些内存不足错误,但我在 logcat 中没有收到相关错误,并且我尝试仅创建一个 jint 数组,而没有任何其他代码,它也会崩溃。

#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;

PS

如果有人能提供从 Java 代码加载 png 的替代方法(没有 pnglib 和像 中那样的本机函数,那就太棒了http://androgeek.info/?p=275

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行雁书 2025-01-10 09:12:19

JNI 中不会自动发生异常。您必须编写代码来触发它们。即它将错误存储在某处,您必须添加代码来说明您希望在何处触发异常。

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


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

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

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

我不知道哪个更好。

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.

做个ˇ局外人 2025-01-10 09:12:19

你需要向我们展示崩溃的情况。 NewIntArray 不应该崩溃;它应该返回 NULL。 (如果 NewIntArray 失败,env->ExceptionCheck() 也将返回 true。)但是崩溃应该告诉您问题是什么,因此您需要显示“adb logcat”输出。

you need to show us the crash. NewIntArray shouldn't crash; it should return NULL. (env->ExceptionCheck() will also return true if NewIntArray failed.) but a crash should tell you what the problem is, so you need to show the "adb logcat" output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文