2
I’m developing a code to get an image of Galeria do Android
. I’m using QtAndroid
to write in Qt a code equivalent to java.
I already opened the gallery, selected the image and received the path, but the return is the MediaColumn
file and I need the absolute path.
How can I pick up the absolute path of the file using QAndroid
to write Qt code equivalent to Java?
Here is the code:
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";
}
}
In general you get a URL with
content://
android, not an absolute path. The only way to actually read this is by using the java API, which would involve creating ajava.io.File
to read the URL. Not very practical, but I believe it is the only way that would always work.– Guilherme Bernal
It is. The problem is I can’t display the image on
QML
using only this reference. How can I do this?– GuiDupas
One option is to use the java api to read the url and write to a file in your cache directory, and then read from there. Or: read the file to a
byte[]
in memory and pass it to the builder ofQPixmap
to upload the image and forward to QML. I don’t have any really good solution.– Guilherme Bernal
And solutions such as this link https://github.com/eswarm/AutoSortD/blob/master/androidfiledialog.cpp ?
– GuiDupas