How to Open Android Image Gallery in Qt

Posted on Posted in Android Tutorials, OpenCV Tutorials, Qt Tutorials, Tutorials

This article describes how to open default Android Gallery in Qt using a mixture of Android’ Java and Qt (C++) code. It is specially useful if you are working with images, for example if you want to open a Mat Image in OpenCV using imread function or if you are just writing some kind of image viewer program. Unfortunately Qt does not offer this (yet) by default so I thought I could share it with you.

I have made a few assumptions such as:

  • You have successfully installed Qt (with Android) on your Computer
  • You have an Android phone or an Emulator (Genymotion and so on) to run Qt Android projects on it.

What it does?

This example project allows you to open an image from Android Gallery and displays it on a QLabel widget. Following screenshot shows Qt Project called QtAndroidGallery that I have shared at the bottom of this post.

QtAndroidGallery





Note: This project (source code) can also be used as a working example of my previous post titled how to mix Android and Qt (C++) code.

How its done?

The idea simply consists of Qt (C++) and Android (Java) code interacting with each other through JNI (Java Native Interface)

Explained in detail:

Below I will explain parts and pieces of the source code that might seem confusing but if you have any questions or facing any issues regarding build or running of this project please start a discussion by commenting down below.


This function allows retrieving of selected file name (from Gallery) after the selection is completed. Note that it is essential to use the exact same pattern seen below in function names for JNI access. Your Domain name, Package name, Java Class and function name all must be separated with “_” as seen here.

JNIEXPORT void JNICALL
Java_com_amin_QtAndroidGallery_QtAndroidGallery_fileSelected(JNIEnv */*env*/,
jobject /*obj*/,
jstring results)
{
selectedFileName = QAndroidJniObject(results).toString();
}


 

This part of the code is used to call an static function in the Java code. “openAnImage” is the name of Java function that takes no parameters and returns the selected file name.

QAndroidJniObject::callStaticMethod<void>(“com/amin/QtAndroidGallery/QtAndroidGallery”,
“openAnImage”,
“()V”);


I will not go into the details of Java code but something that I have seen all over the internet that people have a hard time understanding is how to call a non static function from inside a static function. Simple! 🙂 First create an instance of the same class inside itself, like this (this already exists in the example code)

private static QtAndroidGallery m_instance;
public QtAndroidGallery()
{
m_instance = this;
}

And then, inside that static function, call the non-static function using the instance of the class you created!

static void openAnImage()
{
m_instance.dispatchOpenGallery();
}

 

I won’t go into details of “dispatchOpenGallery” function cause that is pure Android code and you can easily use Android SDK Documentation to get more information on that.


You can download the example project source code from here. It contains all required files for running the project provided that you already have a correct Qt for Android installation.

Download
Qt Android Gallery Example




15 thoughts on “How to Open Android Image Gallery in Qt

  1. Hi,

    I ran your example, Image gallery work but i can open an image. When i selected an image i have this error : W/libQtAndroidGallery.so(25290): (null):0 ((null)): Can’t find surface 1.

    Thank you

    1. You must have a correct Qt Android installation. What is the OpenCV version that you are using?
      Can you please clarify if you are able to see Android Default Gallery showing up or not? Or does it just crash as soon as you try to open?

      1. It doesn’t crash, when is selected an image it close gallery and nothing happend.

        In onActivityResult method, getRealPathFromURI() return me null value.

        1. Sometimes selecting an image from recent pictures in gallery causes such a problem. Did you try selecting a picture from the albums? do not use recent pictures folder

          1. I just double checked and it’s totally working fine. Please let me know your Qt version. And, are you testing with a phone? Emulator? or Genymotion?

          2. Ty this:
            Go to java code and change it as I have shown below. Run the code and see if you see the output.

            String filePath = getRealPathFromURI(getApplicationContext(), data.getData());
            // insert this line
            System.out.println(“debug 123456789”);
            fileSelected(filePath);

  2. I tried with Qt 5.5 on mac and Qt 5.4 on windows both with phone. Kit armeabi and armeabi v7a.

    Ye i see output, and when i put System.out.println(filePath); debug is null.

    1. One more thing. What Android Api version are you using? Try setting to a lower Api. You can change it in AndroidManifest.xml. Also try testing with a phone that has lower Api if you’re phone doesn’t. You can easily install and test this code with Genymotion. It’s free. You just need to build for x86.
      The problem you’re facing sounds like it is because of some change in Api or inconsistence between the devices we are using to test. I’m not sure btw. It would be great if you can test this and let me know the results.

  3. Thanks for your explanation. I wanna ask something more maybe you can write something about it.

    I search internet how to use android studio speciality by Qt but there is nothing.

    I wanna use alarm clock, backround running, connect with google api to use my program google analitics, adwords, google game play etc.

    If you can explain something about these topics it will really useful for me.

    Thanks.

  4. String filePath = getRealPathFromURI(getApplicationContext(), data.getData());
    System.out.println(“debug 123456789”);
    System.out.println(filePath);
    fileSelected(filePath);

    I System.out: debug 123456789
    I System.out: null
    W libQtAndroidGallery.so: (null):0 ((null)): Can’t find surface 1
    W libQtAndroidGallery.so: (null):0 ((null)): Can’t find surface 1

    filePath is null. Dosent Work :/

Leave a Reply

Your email address will not be published. Required fields are marked *