Qt Java - Grab image from gallery

Asked

Viewed 73 times

2

I’m trying to get an image from the Android using Qt. I’ve already called the intent of Java using a arquivo Java, But I don’t know how to return the value to Qt.

I have a class that calls a method in the Java file that opens the Android Image Picker. How can I return the selected image to Qt?

The file . h

#ifndef CAMINHOIMAGENS_H
#define CAMINHOIMAGENS_H

#include <QObject>

#include <QDebug>

class caminhoImagens : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString imagemCaminho READ imagemCaminho NOTIFY imagemCaminhoChanged)

slots:
    void atualizaImagem();

public:
    QString imagemCaminho();

private:
    QString m_imagemCaminho = "";

signals:
    void imagemCaminhoChanged();
};

#endif // CAMINHOIMAGENS_H

the file . cpp

#include "caminhoimagens.h"

#include <QtAndroidExtras>

caminhoImagens::caminhoImagens()
{

}

void caminhoImagens::atualizaImagem()
{
    //org.qtproject.example.acessargaleriatest
    QAndroidJniObject intent = QAndroidJniObject::callStaticObjectMethod (
"org/qtproject/example/AcessarGaleriaTest/ImagePickerAndroid",
 "imagePicker",
 "()Landroid/content/Intent;");

    QtAndroid::startActivity(intent, 192837465, 0);
}

QString caminhoImagens::imagemCaminho()
{
    return m_imagemCaminho;
}

The file . java

package org.qtproject.example.AcessarGaleriaTest;

import org.qtproject.qt5.android.bindings.QtActivity;
import android.content.Intent;
import android.app.Activity;

public class ImagePickerAndroid extends QtActivity
{
    public static Intent imagePicker()
    {
        Intent i = new Intent(Intent.ACTION_PICK);
        i.setType( "image/*");
        return Intent.createChooser(i, "Select Image");
    }
}

1 answer

1


Answering my own question:

I made a code using QAndroidJniObject to write code equivalent to Java.

Register the caminhoimages.h as a QML type in the main.cpp and call the method buscaImagem()

camioimagens. h

#ifndef CAMINHOIMAGENS_H
#define CAMINHOIMAGENS_H

#include <QObject>

#include "imagepickerandroid.h"

#include <QDebug>

class caminhoImagens : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString imagemCaminho READ imagemCaminho NOTIFY imagemCaminhoChanged)

public slots:
    void buscaImagem();
    void retornaImagem(QString path);

public:
    caminhoImagens();

    QString imagemCaminho();

private:
    QString m_imagemCaminho = "";

signals:
    void imagemCaminhoChanged();
};

#endif //CAMINHOIMAGENS_H

camioimagens.cpp

#include "caminhoimagens.h"

caminhoImagens::caminhoImagens()
{

}

void caminhoImagens::buscaImagem()
{
    imagePickerAndroid *imagePicker = new imagePickerAndroid();
    connect(imagePicker, SIGNAL(imagemCaminhoSignal(QString)), this, SLOT(retornaImagem(QString)));

    imagePicker->buscaImagem();
}

void caminhoImagens::retornaImagem(QString path)
{
    qDebug() << path;

    m_imagemCaminho = path;

    emit imagemCaminhoChanged();
}

QString caminhoImagens::imagemCaminho()
{
    return m_imagemCaminho;
}

imagepickerandroid. h

#ifndef IMAGEPICKERANDROID_H
#define IMAGEPICKERANDROID_H


#include <QObject>
#include <QtAndroidExtras>

#include <QDebug>

class imagePickerAndroid : public QObject, public QAndroidActivityResultReceiver
{
    Q_OBJECT

public:
    imagePickerAndroid();

    void buscaImagem();

    virtual void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject & data);

signals:
    void imagemCaminhoSignal(QString);
};

#endif // IMAGEPICKERANDROID_H

imagepickerandroid.cpp

#include "imagepickerandroid.h"

imagePickerAndroid::imagePickerAndroid()
{

}

void imagePickerAndroid::buscaImagem()
{
    QAndroidJniObject ACTION_PICK = QAndroidJniObject::fromString("android.intent.action.GET_CONTENT");
    QAndroidJniObject intent("android/content/Intent");
    if (ACTION_PICK.isValid() && intent.isValid())
    {
        intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", ACTION_PICK.object<jstring>());
        intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("image/*").object<jstring>());
        QtAndroid::startActivity(intent.object<jobject>(), 101, this);
        qDebug() << "OK";
    }
    else
    {
        qDebug() << "ERRO";
    }
}

void imagePickerAndroid::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
{
    qDebug() << "Trabalha com os dados";

    jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
    if (receiverRequestCode == 101 && resultCode == RESULT_OK) {
        QString imagemCaminho = data.callObjectMethod("getData", "()Landroid/net/Uri;").callObjectMethod("getPath", "()Ljava/lang/String;").toString();
        emit imagemCaminhoSignal(imagemCaminho);

        qDebug() << imagemCaminho;
    }
    else
    {
        qDebug() << "Caminho errado";
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.