Passing a string to C-code in Android NDK

11,453

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 ) std::string();

   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 );
}
Share:
11,453
Sreekanth Karumanaghat
Author by

Sreekanth Karumanaghat

Android developer. TechnoCrat,Likes Mathematics,Physics,Programming.Likes to think.

Updated on June 17, 2022

Comments

  • Sreekanth Karumanaghat
    Sreekanth Karumanaghat almost 2 years

    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" );
    }
    
  • Sreekanth Karumanaghat
    Sreekanth Karumanaghat almost 12 years
    Thanks,I want the bitmap as a Jobject and not a file,because there is already a function to retrieve the pixels from the Jobject... Is there any way I can do this?
  • Sergey K.
    Sergey K. almost 12 years
    Use java.nio.ByteBuffer to pass pixels around.
  • Sreekanth Karumanaghat
    Sreekanth Karumanaghat almost 12 years
    I think you got me wrong....either I want to retrieve the bmp as JObject (in NDK)or I need to get a method to get pixels from the file in the NDK.
  • Sergey K.
    Sergey K. almost 12 years
    You cannot get the pixels via jObject in NDK. You will have to pass a ByteBuffer.
  • Sreekanth Karumanaghat
    Sreekanth Karumanaghat almost 12 years
    Sorry I cannot pass ByteBuffer...because that will cause a memory problem...I already have a library that returns the pixels when the JObject corresponding to bmp is passed to it...Is there anyway I can convert the File to JObject inside the NDK?
  • Sergey K.
    Sergey K. almost 12 years
    You cannot convert a FILE to jObject inside NDK.
  • Sreekanth Karumanaghat
    Sreekanth Karumanaghat almost 12 years
  • Rizwan Sohaib
    Rizwan Sohaib over 10 years
    hello Sergey K. I am try to get the string parameters the way you suggesting, but I get UnsatisfiedLikerError. If i keep the parameters of the function to void, then the functions are complied successfully. Can you please help me on this?
  • Sergey K.
    Sergey K. over 10 years
    @RizwanSohaib: Hey, why not to ask a question and put your code there? Don't be shy!
  • Rizwan Sohaib
    Rizwan Sohaib over 10 years
    I am not shy, this is the same question as I have but this solution is not working for me..
  • Sergey K.
    Sergey K. over 10 years
    @RizwanSohaib: then your code is different. Could you post it as a question?
  • Rizwan Sohaib
    Rizwan Sohaib over 10 years
    Ok, I shall post it in some time and will give you the link to review
  • Sergey K.
    Sergey K. over 4 years
    @wEight std::string