WEB开发网
开发学院手机开发Android 开发 Android 从 JNI 中传入 UTF8 的字符到 Java 阅读

Android 从 JNI 中传入 UTF8 的字符到 Java

 2010-03-26 21:14:00 来源:WEB开发网   
核心提示:jbyte buffer[] = /* UTF8 encoding buffer */jbyteArray bytes = env->NewByteArray(sizeof(buffer));env->SetByteArrayRegion(bytes, 0, sizeof(buffer), buffer);

jbyte buffer[] = /* UTF8 encoding buffer */

jbyteArray bytes = env->NewByteArray(sizeof(buffer));

env->SetByteArrayRegion(bytes, 0, sizeof(buffer), buffer);

return bytes;

}

JNIEXPORT jbyteArray JNICALL Java_ReturnUTF8_method2

(JNIEnv *env, jclass cls)

{

jbyte buffer[] = /* UTF8 encoding buffer */

jbyteArray bytes = env->NewByteArray(sizeof(buffer));

env->SetByteArrayRegion(bytes, 0, sizeof(buffer), buffer);

jchar encoding_name[] = { 'U', 'T', 'F', '-', '8', '' };

jstring encoding = env->NewString(encoding_name, 5);

jclass theStringClass = env->FindClass("Ljava/lang/String;");

jmethodID mid = env->GetMethodID(theStringClass, "", "([BLjava/lang/String;)V");

jobject result = env->NewObject(theStringClass, mid, bytes, encoding);

return result;

}

#include

"ReturnUTF8.h"

JNIEXPORT jbyteArray JNICALL Java_ReturnUTF8_method1

(JNIEnv *env, jclass cls)

{

jbyte buffer[] = /* UTF8 encoding buffer */

jbyteArray bytes = env->NewByteArray(sizeof(buffer));

env->SetByteArrayRegion(bytes, 0, sizeof(buffer), buffer);

return bytes;

}

JNIEXPORT jbyteArray JNICALL Java_ReturnUTF8_method2

(JNIEnv *env, jclass cls)

{

jbyte buffer[] = /* UTF8 encoding buffer */

jbyteArray bytes = env->NewByteArray(sizeof(buffer));

env->SetByteArrayRegion(bytes, 0, sizeof(buffer), buffer);

jchar encoding_name[] = { 'U', 'T', 'F', '-', '8', '' };

jstring encoding = env->NewString(encoding_name, 5);

jclass theStringClass = env->FindClass("Ljava/lang/String;");

jmethodID mid = env->GetMethodID(theStringClass, "",

"([BLjava/lang/String;)V");

jobject result = env->NewObject(theStringClass, mid, bytes,

encoding);

return result;

}

# g++ -shared -I /usr/lib/jvm/java-1.5.0-sun/include -I /usr/lib/jvm/java-1.5.0-sun/include/linux ReturnUTF8.cpp -o libReturnUTF8Jni.so

# export LD_LIBRARY=.

# java ReturnUTF8

上一页  1 2 

Tags:Android JNI 传入

编辑录入:coldstar [复制链接] [打 印]
赞助商链接