How to test if a file exist on Android when the url comes from QML FileDialog?
-
wrote on 24 Jan 2022, 18:30 last edited by
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)
, theQFile::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?
-
wrote on 24 Jan 2022, 18:39 last edited by mbruel
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? -
wrote on 25 Jan 2022, 10:25 last edited by
Nobody?
I've find a solution (using this thread) but it involves to copy the image loaded in QML via a C++ method intoQStandardPaths::writableLocation(QStandardPaths::AppDataLocation)
I obtain a "proper" path for my copied image that can be converted to url for QML usingQUrl::fromLocalFile
I'd prefer not to do the copy, so if anyone have another solution, I'm still interested ;)
-
wrote on 25 Jan 2022, 15:53 last edited by fcarney
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?
-
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?
wrote on 25 Jan 2022, 17:14 last edited by@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/5