简体   繁体   中英

c++ vector<string> to jstring [] (the thava String[]) conversion

from a c++ source code, I'm invoking a java method that takes as argument a String []. (More exactly is the "EXTRA_MAIL field of the JIntent ACTION_SEND). How can I convert a vector (or just two char*) into a Java "String[]" object? I know that in order to convert a c++ string to a "Java String" (just one string) there is a "StringToJSring" function, but what about String []? Thanx in advance.

To be more detailed, I'm trying to run the following code from a c++ builder firemonkey application for android:

#include <Androidapi.JNI.GraphicsContentViewText.hpp>
#include <Androidapi.JNI.Net.hpp>
#include <Androidapi.Helpers.hpp>
#include <FMX.Helpers.Android.hpp>

button callback (onclick):
_di_JIntent  email =  TJIntent::JavaClass->init(TJIntent::JavaClass->ACTION_SEND);
email->setType(StringToJString("plain/text"));

jstring emptyString = TJNIResolver::GetJNIEnv()->NewStringUTF("");
jobjectArray data = (jobjectArray)TJNIResolver::GetJNIEnv()->NewObjectArray(2, TJNIResolver::GetJNIEnv()->FindClass("java/lang/String"), emptyString);

TJNIResolver::GetJNIEnv()->SetObjectArrayElement( data,0,TJNIResolver::GetJNIEnv()->NewStringUTF("xxx@uuu.com"));
TJNIResolver::GetJNIEnv()->SetObjectArrayElement( data,1,TJNIResolver::GetJNIEnv()->NewStringUTF("zzz@uuu.com"));

email->putExtra(TJIntent::JavaClass->EXTRA_EMAIL,  data);
email->putExtra(TJIntent::JavaClass->EXTRA_SUBJECT, StringToJString("My Subject"));
email->putExtra(TJIntent::JavaClass->EXTRA_TEXT, StringToJString("My text"));
SharedActivity()->startActivity(TJIntent::JavaClass->createChooser(email, StringToJString("Choose an Email client :")));

You can create an array of any class with env->NewObjectArray()`:

// Find the String class
jclass stringClass = (*env)->FindClass(env, "java/lang/String");

// Create a String[2]
jobjectArray arr = (*env)->NewObjectArray(env, 2, stringClass, NULL);

// Add items
(*env)->SetObjectArrayElement(env, arr, 0, stringObj1);
(*env)->SetObjectArrayElement(env, arr, 1, stringObj2);

Note that this is untested and might need some adjusting. For more info see the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM