qt 和java混合编程

用QAndroidJniObject来做new一个对象:

java Qt C++
File file = new File(); QAndroidJniObject file(“java/io/File”, “()V”);
File file = new File(“/tmp/a.txt”); QAndroidJniObject str = QAndroidJniObject::fromString(“/tmp/a.txt”); QAndroidJniObject file(“java/io/File”, “(Ljava/lang/String;)V”, str.object());
intent.putExtra(MediaStore.EXTRA_OUTPUT, takePhotoSavedUri); intent.callObjectMethod(“putExtra”,”(Ljava/lang/String;Landroid/os/Parcelable;)Landroid/content/Intent;”, MediaStore__EXTRA_OUTPUT.object(), takePhotoSavedUri.object());
boolean Z
byte B
char C
short S
int I
long J
float F
double D
完整类名 L完整类名/分隔;
类型[] [类型
函数类型 (多个参数类型)返回类型

代码转换例子

String action = "android.media.action.IMAGE_CAPTURE";
Intent intent = new Intent(action);

File extDir = Environment.getExternalStorageDirectory();
String filename = "camera.jpg";
File photo = new File(extDir, filename);

Uri takePhotoSavedUri = Url.fromFile( photo );

intent.putExtra( MediaStore.EXTRA_OUTPUT, takePhotoSavedUri );

int SHOOT_PHOTO = 1;
startActivityForResult(intent, SHOOT_PHOTO);
void MyController::callExternalCamera()
{
#ifdef Q_OS_ANDROID
    //android.provider.MediaStore.EXTRA_OUTPUT
    QAndroidJniObject MediaStore__EXTRA_OUTPUT
            = QAndroidJniObject::getStaticObjectField(
                "android/provider/MediaStore", "EXTRA_OUTPUT", "Ljava/lang/String;");
    qDebug() << "MediaStore__EXTRA_OUTPUT.isValid()=" << MediaStore__EXTRA_OUTPUT.isValid();

    QAndroidJniObject action = QAndroidJniObject::fromString("android.media.action.IMAGE_CAPTURE");
    QAndroidJniObject intent=QAndroidJniObject("android/content/Intent","(Ljava/lang/String;)V",
                                               action.object());
    qDebug() << __FUNCTION__ << "intent.isValid()=" << intent.isValid();

    QAndroidJniObject extDir = QAndroidJniObject::callStaticObjectMethod(
                "android/os/Environment", "getExternalStorageDirectory", "()Ljava/io/File;");
    qDebug() << "extDir.isValid()=" << extDir.isValid();

    QAndroidJniObject filename = QAndroidJniObject::fromString("camera.jpg");

    QAndroidJniObject photo=QAndroidJniObject("java/io/File","(Ljava/io/File;Ljava/lang/String;)V",
                                               extDir.object(), filename.object());
    qDebug() << __FUNCTION__ << "photo.isValid()=" << photo.isValid();

    takePhotoSavedUri = QAndroidJniObject::callStaticObjectMethod(
                "android/net/Uri", "fromFile", "(Ljava/io/File;)Landroid/net/Uri;", photo.object());
    qDebug() << "takePhotoSavedUri.isValid()=" << takePhotoSavedUri.isValid();

    intent.callObjectMethod(
                "putExtra","(Ljava/lang/String;Landroid/os/Parcelable;)Landroid/content/Intent;",
                MediaStore__EXTRA_OUTPUT.object(), takePhotoSavedUri.object());
    qDebug() << __FUNCTION__ << "intent.isValid()=" << intent.isValid();

    int SHOOT_PHOTO = 1;
    QtAndroid::startActivity(intent, SHOOT_PHOTO, this);
#endif

}

上面只是直译,实际你想调用时直接调用方法即可.

class MyActivity extends org.qtproject.qt5.android.bindings.QtActivity
{
    public void callExternalCamera()
    {
        String action = "android.media.action.IMAGE_CAPTURE";
        Intent intent = new Intent(action);

        File extDir = Environment.getExternalStorageDirectory();
        String filename = "camera.jpg";
        File photo = new File(extDir, filename);

        Uri takePhotoSavedUri = Uri.fromFile( photo );

        intent.putExtra( MediaStore.EXTRA_OUTPUT, takePhotoSavedUri );

        int SHOOT_PHOTO = 1;
        startActivityForResult(intent, SHOOT_PHOTO);
    }
}
 QAndroidJniObject activity = QtAndroid::androidActivity();

 activity.callMethod<void>("callExternalCamera","()V");

你可能感兴趣的:(c++数据类型)