1

I am creating an App. I do not have any UI, if there is any, I am using OpenGL for that. So in short, I do not have any Java code. I am using NativeActivity sample for reference.

My need is to get Camera Path, which is device dependent. In Apps, where there is Java Activity available, it simple as stated in Here.

Is there any way to get get same functionality in completely native code? Is there an alternative?

I do not really want to add a java activity, since I have already created something with OpenGL. Adding Java Acivity only to get this string makes me feel fool over the period of time.

Community
  • 1
  • 1

1 Answers1

0

You can use JNI from your native code to call the Java methods you want.

Try this from your native activity:

JNIEnv *env; 
state->activity->vm->AttachCurrentThread(&env, NULL); 

jclass envClass = env->FindClass("android/os/Environment");
jmethodID getExtStorageDirectoryMethod = env->GetStaticMethodID(envClass, "getExternalStorageDirectory",  "()Ljava/io/File;");
jobject extStorageFile = env->CallStaticObjectMethod(envClass, getExtStorageDirectoryMethod);

jclass fileClass = env->FindClass("java/io/File");
jmethodID getPathMethod = env->GetMethodID(fileClass, "getPath", "()Ljava/lang/String;");
jstring extStoragePath = env->CallObjectMethod(extStorageFile, getPathMethod);
const char* extStoragePathString = env->GetStringUTFChars(extStoragePath);

//use extStoragePathString

env->ReleaseStringUTFChars(extStoragePath, extStoragePathString);
state->activity->vm->DetachCurrentThread();

If you want to use more Java methods, you may want to look into subclassing NativeActivity from Java and using a Java Helper class, like it's done inside the Teapot sample from the NDK.

ph0b
  • 14,353
  • 4
  • 43
  • 41