How to covert UTF8 string to UTF16 in JNI
- by Er Rahul Rajkumar Gupta
Can anyone please tell me that what is going on wrong with me in this code.Actually in following line of codes I am taking the path of sdcard in a string in jni (C code) and in concatenate function concatenating these manually using loop.The string returned by concatenate works fine but when I am converting it to jstring it prints garbage value in my logcat.
Kindly tell me what is the problem.
jstring str=(jstring)env->CallObjectMethod(sdcard,storagestring);
const char jclass cfile=env->FindClass("java/io/File");
jmethodID fileid=env->GetMethodID(cfile,"<init>","(Ljava/lang/String;)V");
jclass envir=env->FindClass("android/os/Environment");
jmethodID storageid=env->GetStaticMethodID(envir,"getExternalStorageDirectory","()Ljava/io/File;");
jobject sdcard=env->CallStaticObjectMethod(envir,storageid);
jclass sdc=env->GetObjectClass(sdcard);
jmethodID storagestring=env->GetMethodID(sdc,"toString","()Ljava/lang/String;");
*nativeString = env->GetStringUTFChars(str, 0);
char *s =concatenate(nativeString,"/f1.3gp");
//fpath=s;
fpath=env->NewStringUTF(s);
jobject fobject=env->NewObject(cfile,fileid,fpath);
LOGI("size of char=%d size of string=%d",sizeof("/f1.3gp"),sizeof(fpath));
jmethodID existid=env->GetMethodID(cfile,"exists","()Z");
if(env->CallBooleanMethod(fobject,existid))
{
jmethodID delid=env->GetMethodID(cfile,"delete","()Z");
if(env->CallBooleanMethod(fobject,delid))
LOGE("File is deleting...%s",env->NewStringUTF("/f1.3gp"));
}
jmethodID newfileid=env->GetMethodID(cfile,"createNewFile","()Z");
if(env->CallBooleanMethod(fobject,newfileid))
LOGE("dig dig %s",fpath);
jthrowable exc=env->ExceptionOccurred();
if(exc)
{
env->ExceptionDescribe();
env->ExceptionClear();
}
LOGE("creating file %s",fpath);
}