Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Android/ios image picker
Forum Updated to NodeBB v4.3 + New Features

Android/ios image picker

Scheduled Pinned Locked Moved QML and Qt Quick
14 Posts 7 Posters 7.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    Yes it does. For Android, I haven't tested it but you would probably need to use JNI for the same thing (qandroidextras comes to mind)

    There's only so much people working on Qt and there are priorities they have to follow. Anyway it's something that might appear in the extras repository in the future :)

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AlterX
      wrote on last edited by
      #5

      Anyway I was not able to find ios code...

      Qt Ambassador
      Real-time cooperative teams: http://www.softairrealfight.net
      Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

      https://codereview.qt-project.org/...

      1 Reply Last reply
      0
      • GianlucaG Offline
        GianlucaG Offline
        Gianluca
        wrote on last edited by
        #6

        For iOS you need to use UIImagePickerController, and because it's very easy to mix Qt C++ code with Objective-C code, just look with google on a tutorial for using UIImagePickerController and you can directly use that code.

        For Android the road it's a bit more difficult, this is my solution:

        • Create a custom android java Activity subclassing QtActivity, into the java class define some static method for constructing the necessary Intent:
          @
          public static Intent createChoosePhotoIntent() {
          Intent intent = new Intent( Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
          intent.setType("image/*");
          return Intent.createChooser(intent, "Select Image");
          }
          @

        • Create a C++ subclass of QAndroidActivityResultReceiver for getting the result of the Intent launched (in this case the images chosen):
          @
          class OnSelectPhotoResult : public QAndroidActivityResultReceiver {
          void handleActivityResult(int requestCode, int resultCode, const QAndroidJniObject & data) {
          qDebug() << "TOTAPE RETURNING FROM SELECT PHOTO - " << resultCode << " - " << requestCode;
          if ( resultCode == -1 ) {
          // OK !
          QAndroidJniObject imageUri = data.callObjectMethod(
          "getData",
          "()Landroid/net/Uri;");
          QAndroidJniObject imageFile = QAndroidJniObject::callStaticObjectMethod(
          "com/totape/app/TotapeActivity",
          "getPath",
          "(Landroid/net/Uri;)Ljava/lang/String;",
          imageUri.object<jobject>());
          Backend::instance()->photoReady( imageFile.toString() );
          }
          }
          };
          @
          In my code above, I use java methods to process the result, this because the returned data are java object, so you need to call java methods via JNI in order to extract information. For doing so, personally I prefer to create static method in the custom Activity in order to minimize the number of JNI calls

        • Launch the Intent from C++ with this code:
          @
          void Backend::selectPhoto() {
          QAndroidJniObject intent = QAndroidJniObject::callStaticObjectMethod(
          "com/totape/app/TotapeActivity",
          "createChoosePhotoIntent",
          "()Landroid/content/Intent;" );
          qDebug() << "STARTING INTENT FOR SELECTING A PHOTO";
          QtAndroid::startActivity( intent, 12051978, data->onSelectPhoto );
          }
          @

        That's it.
        This is my solution.

        M 1 Reply Last reply
        0
        • A Offline
          A Offline
          AlterX
          wrote on last edited by
          #7

          Nice solotuion on Android...I was expecting something related to create my activity.

          Ok thanks,
          I will give a try to that.

          Qt Ambassador
          Real-time cooperative teams: http://www.softairrealfight.net
          Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

          https://codereview.qt-project.org/...

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AlterX
            wrote on last edited by
            #8

            Hello thanks,
            I am trying to create the code, but I don't understand what is
            data->onSelectPhoto parameter in QtAndroid::startActivity() function.

            Could you tell me a bit more about that?

            Thanks
            Gianni

            Qt Ambassador
            Real-time cooperative teams: http://www.softairrealfight.net
            Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

            https://codereview.qt-project.org/...

            M 1 Reply Last reply
            0
            • A AlterX

              Hello thanks,
              I am trying to create the code, but I don't understand what is
              data->onSelectPhoto parameter in QtAndroid::startActivity() function.

              Could you tell me a bit more about that?

              Thanks
              Gianni

              M Offline
              M Offline
              malleeswarareddy
              wrote on last edited by
              #9

              how to get the result from handleActivityResult virtual method ,when i select any action using android jni.

              1 Reply Last reply
              0
              • GianlucaG Gianluca

                For iOS you need to use UIImagePickerController, and because it's very easy to mix Qt C++ code with Objective-C code, just look with google on a tutorial for using UIImagePickerController and you can directly use that code.

                For Android the road it's a bit more difficult, this is my solution:

                • Create a custom android java Activity subclassing QtActivity, into the java class define some static method for constructing the necessary Intent:
                  @
                  public static Intent createChoosePhotoIntent() {
                  Intent intent = new Intent( Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
                  intent.setType("image/*");
                  return Intent.createChooser(intent, "Select Image");
                  }
                  @

                • Create a C++ subclass of QAndroidActivityResultReceiver for getting the result of the Intent launched (in this case the images chosen):
                  @
                  class OnSelectPhotoResult : public QAndroidActivityResultReceiver {
                  void handleActivityResult(int requestCode, int resultCode, const QAndroidJniObject & data) {
                  qDebug() << "TOTAPE RETURNING FROM SELECT PHOTO - " << resultCode << " - " << requestCode;
                  if ( resultCode == -1 ) {
                  // OK !
                  QAndroidJniObject imageUri = data.callObjectMethod(
                  "getData",
                  "()Landroid/net/Uri;");
                  QAndroidJniObject imageFile = QAndroidJniObject::callStaticObjectMethod(
                  "com/totape/app/TotapeActivity",
                  "getPath",
                  "(Landroid/net/Uri;)Ljava/lang/String;",
                  imageUri.object<jobject>());
                  Backend::instance()->photoReady( imageFile.toString() );
                  }
                  }
                  };
                  @
                  In my code above, I use java methods to process the result, this because the returned data are java object, so you need to call java methods via JNI in order to extract information. For doing so, personally I prefer to create static method in the custom Activity in order to minimize the number of JNI calls

                • Launch the Intent from C++ with this code:
                  @
                  void Backend::selectPhoto() {
                  QAndroidJniObject intent = QAndroidJniObject::callStaticObjectMethod(
                  "com/totape/app/TotapeActivity",
                  "createChoosePhotoIntent",
                  "()Landroid/content/Intent;" );
                  qDebug() << "STARTING INTENT FOR SELECTING A PHOTO";
                  QtAndroid::startActivity( intent, 12051978, data->onSelectPhoto );
                  }
                  @

                That's it.
                This is my solution.

                M Offline
                M Offline
                Mitchell
                wrote on last edited by
                #10

                @Gianluca Can you explain the code for the android work around a little more. I am confused about what Backend does as well as what files the code should be in. Such as making a c++ subclass as well as making a subclass of QtActivity.

                thank you

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mitchell
                  wrote on last edited by
                  #11

                  In the QtAndroid::startActivity() function data->onSelectPhoto what is data? You don't have it defined anywhere. I have tried QAndroidJniObject and QAndroidActivityresultHandler but it says onSelectPhoto is not a member of these types. Any help would be great!!!

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    amahta
                    wrote on last edited by
                    #12

                    There is a workaround for getting images from Android Gallery. You can check this post for more info.

                    Thou shalt programme
                    http://www.amin-ahmadi.com

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      AlterX
                      wrote on last edited by
                      #13

                      Thanks for android I solved it. It is working very well.

                      Qt Ambassador
                      Real-time cooperative teams: http://www.softairrealfight.net
                      Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

                      https://codereview.qt-project.org/...

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        h00bs
                        wrote on last edited by
                        #14

                        This is an old thread, but maybe still interesting. There is an (multi-) image picker component for Android and iOS available with Felgo: https://felgo.com/updates/release-3-2-0-qt-5-12-3-subscriptions

                        Developer at Felgo - https://felgo.com/qt

                        Develop mobile Apps for iOS & Android with Qt
                        Develop Games with Qt

                        Felgo is an official Qt Technology Partner

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved