Issue
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);
Solution
The reason this isn't working for you is that you need to have a SECOND native method for the code I gave above. That second native method, you can call from onActivityResult() AFTER user selects the file. That means when the getUserSelectedFile() is called, the String will already have the value.
Note: Answer is edited few times based on comments below.
In your activity:
String selectedFilePath = "";
static {
System.loadLibrary("your_lib_name");
}
public native void getImagePath();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 200 && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
selectedFilePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
cursor.close();
getImagePath(); // this is your native method that will invoke getUserSelectedFile()
}
}
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 );
}
private static String getUserSelectedFile() {
return selectedFilePath;
}
Then in native (C/C++), you need these two methods:
void Java_com_my_package_name_pickImage(JNIEnv *env, jobject obj) {
jclass clazz(env->GetObjectClass(activity));
jmethodID method_id = env->GetMethodID(clazz, "pickImage", "()V");
env->CallVoidMethod(activity, method_id);
}
void Java_com_my_package_name_getImagePath(JNIEnv *env, jobject obj) {
jclass clazz(env->GetObjectClass(activity));
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);
}
I hope this is clearer.
Answered By - ᴛʜᴇᴘᴀᴛᴇʟ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.