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. How to test if a file exist on Android when the url comes from QML FileDialog?
Forum Update on Monday, May 27th 2025

How to test if a file exist on Android when the url comes from QML FileDialog?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 936 Views
  • 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.
  • M Offline
    M Offline
    mbruel
    wrote on 24 Jan 2022, 18:30 last edited by
    #1

    Hi,
    I know that normally Qt.labs.platform.FileDialog doesn't support Android but it is kind of working by opening the default file browser.
    I'm opening an image:

        FileDialog {
            id: imgDialog
            title: "Select an image file"
            folder: StandardPaths.standardLocations(StandardPaths.PicturesLocation)[0]
            nameFilters: "Image files (*.png *.jpeg *.jpg)"
            onAccepted: {
                print("new picture: "+file);
                dogPictureFile = file;
            }
        } // imgDialog
    

    On my Android phone, a Xiaomi, I'm getting this url: content://com.android.providers.media.documents/document/image%3A455944

    I'm saving this url in a Database and when I restart the app to load it, I'm using this C++ method:

    QString Dog::pictureUrl() const
    {
        if (_pictureUrl.isEmpty())
            return "images/dogIcon.png";
    
        QUrl url(_pictureUrl);
        QString path = url.path();
        qDebug() << "path of picture '"<< _pictureUrl << "' : " << path;
    
    #if defined(Q_OS_ANDROID)
        return url.toString();
    #else
        return QFile::exists(path) ? url.toString() : "images/dogIcon.png";
    #endif
    }
    

    As you can see I don't know how to test if the image using the QML url exists...
    If I don't have the #if defined(Q_OS_ANDROID), the QFile::exists(path) returns false and I'm getting the default picture :'(
    Here is the debug output with the Path: path of picture ' "content://com.android.providers.media.documents/document/image%3A455944" ' : "/document/image:455944"

    What can I do? any idea?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbruel
      wrote on 24 Jan 2022, 18:39 last edited by mbruel
      #2

      PS: if I delete the image, I'm getting this warning message:
      W MyApp: qrc:/MyApp.qml:48:5: QML Image: Cannot open: content://com.android.providers.media.documents/document/image%3A455944
      it would be great if I could catch a signal or something but I suppose there not this kind of thing in QML... is it?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbruel
        wrote on 25 Jan 2022, 10:25 last edited by
        #3

        Nobody?
        I've find a solution (using this thread) but it involves to copy the image loaded in QML via a C++ method into QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)
        I obtain a "proper" path for my copied image that can be converted to url for QML using QUrl::fromLocalFile

        I'd prefer not to do the copy, so if anyone have another solution, I'm still interested ;)

        1 Reply Last reply
        1
        • F Offline
          F Offline
          fcarney
          wrote on 25 Jan 2022, 15:53 last edited by fcarney
          #4

          You could try loading the entire URL into a hidden Image. Check the status using the onStatus method and check for null or error conditions. Then make decision about what URL is used to be displayed.

          https://doc.qt.io/qt-5/qml-qtquick-image.html#status-prop

          Edit: Does that URL resolve when used with Image?

          C++ is a perfectly valid school of magic.

          M 1 Reply Last reply 25 Jan 2022, 17:14
          1
          • F fcarney
            25 Jan 2022, 15:53

            You could try loading the entire URL into a hidden Image. Check the status using the onStatus method and check for null or error conditions. Then make decision about what URL is used to be displayed.

            https://doc.qt.io/qt-5/qml-qtquick-image.html#status-prop

            Edit: Does that URL resolve when used with Image?

            M Offline
            M Offline
            mbruel
            wrote on 25 Jan 2022, 17:14 last edited by
            #5

            @fcarney
            perfect that's exactly what I needed! my bad, I didn't go to the bottom of the documentation...
            Yes QML Image loads perfectly this kind of url: content://com.android.providers.media.documents/document/image%3A455947
            So I'm just storing it as a string and do the test in QML rather than in C++. No need of an hidden image, it can be done directly on the visible one with something like this:

                Image {
                    id: dogPicture
                    fillMode: Image.PreserveAspectFit;
                    height: parent.height/2 - 8* spacing
                    source: dog.pictureUrl
                    anchors {
                        top: parent.top
                        topMargin : spacing
                        horizontalCenter: parent.horizontalCenter
                    }
                    onStatusChanged: if (status === Image.Error) source = dog.dogIconDefault();
                }
            

            with my C++ accessor being:

            QString Dog::pictureUrl() const
            {
                if (_pictureUrl.isEmpty())
                    return sDogIconDefault;
                return QUrl(_pictureUrl).toString();
            }
            

            Thanks @fcarney ;)

            1 Reply Last reply
            1

            1/5

            24 Jan 2022, 18:30

            • Login

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