I am trying to get an image path through NDK using an intent in Android. I have the intent below that will pick an image from a Gallery. How can I invoke it through NDK and get the result back in C++? The code below is part of my MainActivity class.
public void pickImage(Activity activity)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), 200 );
}
String selectedFilePath = "";
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri path = data.getData();
selectedFilePath = path.toString();
}
public String getUserSelectedFile() {
return selectedFilePath;
}
On the C++ side I can call the intent using the code below but how can I get the result from onActivityResult? What I am getting on the first run is empty string from selectedFilePath, but when I call the intent second time I am getting the string from before. I think that pickImage is blocking getUserSelectedFile.
jclass clazz(env->GetObjectClass(activity));
jmethodID method_id = env->GetMethodID(clazz, "pickImage", "()V");
env->CallVoidMethod(activity, method_id);
jmethodID method_id2 = env->GetMethodID(clazz, "getUserSelectedFile", "()Ljava/lang/String;");
jobject result = env->CallObjectMethod(activity, method_id2);
const char *strReturn = env->GetStringUTFChars((jstring) result, 0);