Qt Java - Get gallery image

2

I'm trying to get an image from the gallery of Android using Qt. I've already called intent of Java using a arquivo Java , but I do not know how to return the value to Qt.

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

The .h file

#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 .cpp file

#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 .java file

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");
    }
}
    
asked by anonymous 26.11.2015 / 17:43

1 answer

1

Answering my own question:

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

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

pathimages.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

pathimagens.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";
    }
}
    
30.11.2015 / 18:43