Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QFileDialog::getOpenFileName always returns empty string on Android
As the title says, I have working code (on Windows & Linux) that I compiled for Android. Everything works, except that when I try to open a local file, I cannot get file path back from getOpenFileName(). I do get the native Android (Android 8) file picker dialogue for local files, and I can select anything.
Yet the return value is always an empty string. This happens with Qt 5.13.2 and 5.14.2, and with a somewhat older SDK & NDK, as well as the (as of writing) latest SDK & NDK.
It's essentially the same issue as in this thread: https://forum.qt.io/topic/109548/android-qfiledialog-returns-nothing-and-code-in-background-keeps-running
That thread never got a useful response or resolution, however. Am I to conclude that opening local files on Android using any of the QFileDialog methods is not functional on Android? The documentation sadly doesn't even list Android (or iOS).
It's been a while since I implemented file dialog in an Android app, so Unfortunately I don't have exact reply here. But maybe it will at least help you a bit.
Here's what you (may) need:
- make sure you request appropriate file access permissions in your Android manifest (WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE
- use a file provider (android/res/xml/fileprovider.xml) with appropriate adnotations in AndroidManifest for it
- use native code (Java) to "extract" file data from file provider using custom Intent
- make sure to do a runtime check if user has granted the permission (
Sorry for being so vague, unfortunately I'm not allowed to share the code I've written (and it was written for different purpose - I'm not exactly certain it would work). Unfortunately, Android keeps making it harder and harder to access files :-(
Oh, a much better answer came up in this thread.
@sierdzio So essentially the issue is that Qt still uses Intents on Android for things like the filepicking dialogue, but doesn't implement the result handling?
This seems like something that should be documented in the QFileDialog documentation and such places.
I'm confused about in how far the "Sharing Files on Android or iOS from your Qt App" article applies as well here. Since calling those static functions in QFileDialog already seem to do the calling of an Intent, that would mean that one 'just' needs to add the part where one handles the result, no?
My feeling here is still that this qualifies as a bug in Qt, if not of QFileDialog, then of the documentation.
You're very likely correct that it is a bug :-)
I have filed a bug report over at: https://bugreports.qt.io/browse/QTBUG-83372
Thanks for the report !
Would you mind uploading your example directly on the report ? That allows to have everything in a single place.
@SGaist You mean the entire Qt project?
koahnig last edited by
Typically a small project showing the issue is enough.
If the developers have already such a small project it give them lead where to look at.
@koahnig I have attached a simple demonstration project to the bug report.
Omg, Miss Maya Posch
believe or not, your post been mention a million times :)
@mrjj I'll believe it, based on the traffic stats to my programming blog :)
Hehe yeah and comments all the way from 2011 to 2019 :)
@mrjj Quite a few comments that I should be looking into to update the blog post, too, perhaps :)
Sadly it seems that Qt even after 9 years hasn't seen the need to meaningfully update their documentation for things like QThread. Now with Android the situation is even worse, with very little documentation to be found.
At any rate I hope that this bug in QFileDialog can be resolved soon, because right now it's a blocker for Qt on Android in my project. I may have to go back to just using Java there again, which would be somewhat sad.
Well yes, perhaps :)
well they did add a worker example to the docs
Also after years of shouting at people that would subclass QThread, then the fight died out with
So the docs show both ways.
Well bug fixing is a bit slow so might take some time to get a fix for the android
QFileDialog thing but it should come at some point.
I have not done much with java, but recently fooled around with Flutter and
found it very nice to work with. If only it had a WYSIWYG editor i would be sold. :)
But yeah, it would be sad your Qt project would be blocked and abandoned just due to a silly bug.