Passing a string to C-code in Android NDK

The program should take an Image from the SD card and adjust its brightness. And the image is taken from the SD card via the NDK C-code. It is to be noted that the string depicting the path to the image is passed to the NDK via JNI.

Java code:

private void adjustBrightness() {
   imagePath     = (Environment.getExternalStorageDirectory().getPath()+"earthglobe.jpeg").toCharArray();
   brightness(imagePath, brightness);

public native void brightness(char[] imagePath, float brightness);

NDK code:

JNIEXPORT void JNICALL Java_com_example_ImageActivity_brightness(JNIEnv * env,char[] bitmappath, jfloat brightnessValue)
   string bmpath    =   bitmappath+'\0';
   jobject  obj = fopen( bitmappath , "rb" );


You cannot pass char[] this way.

In Java use:

public static native void brightness(String imagePath, float brightness);

In native use:

std::string ConvertJString(JNIEnv* env, jstring str)
   if ( !str ) LString();

   const jsize len = env->GetStringUTFLength(str);
   const char* strChars = env->GetStringUTFChars(str, (jboolean *)0);

   std::string Result(strChars, len);

   env->ReleaseStringUTFChars(str, strChars);

   return Result;

JNIEXPORT void JNICALL Java_com_example_ImageActivity_brightness(JNIEnv * env, jobject obj, jstring bitmappath, jfloat brightnessValue)
   std::string bmpath = ConvertJString( env, bitmappath );
   FILE* f = fopen( bmpath.c_str(), "rb" );

   // do something useful here

   fclose( f );

Need Your Help

Add 90 min to NSDate

ios time

what is wrong with this code?