Skip to content
  • File operation

    Unsolved General and Desktop
    2
    0 Votes
    2 Posts
    667 Views
    mrjjM

    @Ashwinimehra
    Hi and welcome
    Its outside the scope of Qt but linux does support it
    http://www.thegeekstuff.com/2012/04/linux-file-locking-types/

  • 0 Votes
    2 Posts
    2k Views
    guidupasG

    SOLVED

    Here is the code to get the full image path from android gallery using java equivalent code written with Qt:

    #include "imagepickerandroid.h" imagePickerAndroid::imagePickerAndroid() { } void imagePickerAndroid::buscaImagem() { QAndroidJniObject ACTION_PICK = QAndroidJniObject::getStaticObjectField("android/content/Intent", "ACTION_PICK", "Ljava/lang/String;"); QAndroidJniObject EXTERNAL_CONTENT_URI = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$Images$Media", "EXTERNAL_CONTENT_URI", "Landroid/net/Uri;"); QAndroidJniObject intent=QAndroidJniObject("android/content/Intent", "(Ljava/lang/String;Landroid/net/Uri;)V", ACTION_PICK.object<jstring>(), EXTERNAL_CONTENT_URI.object<jobject>()); if (ACTION_PICK.isValid() && intent.isValid()) { 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) { jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK"); if (receiverRequestCode == 101 && resultCode == RESULT_OK) { QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;"); QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$MediaColumns", "DATA", "Ljava/lang/String;"); QAndroidJniEnvironment env; jobjectArray projecao = (jobjectArray)env->NewObjectArray(1, env->FindClass("java/lang/String"), NULL); jobject projacaoDadosAndroid = env->NewStringUTF(dadosAndroid.toString().toStdString().c_str()); env->SetObjectArrayElement(projecao, 0, projacaoDadosAndroid); QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;"); QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), projecao, NULL, NULL, NULL); jint columnIndex = cursor.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", dadosAndroid.object<jstring>()); cursor.callMethod<jboolean>("moveToFirst", "()Z"); QAndroidJniObject resultado = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex); QString imagemCaminho = "file://" + resultado.toString(); emit imagemCaminhoSignal(imagemCaminho); } else { qDebug() << "Caminho errado"; } }
  • 0 Votes
    4 Posts
    3k Views
    guidupasG

    Thanks for the help @Vi67

    I have done my code in Qt using QAndroidJniObject to write a equivalent java code.

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

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

    caminhoimagens.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"; } }
  • 0 Votes
    2 Posts
    2k Views
    guidupasG

    Solved.
    I used a condition: android { QT += androidextras } in the .pro file

  • 0 Votes
    1 Posts
    997 Views
    No one has replied