[Interest] Is there a full FileDialog example for Android with writing and saving files?
Marius Kittler
mkittler at suse.de
Fri Oct 18 22:29:47 CEST 2024
> W DRC : java.lang.SecurityException: Permission Denial: reading
> com.android.externalstorage.ExternalStorageProvider uri
> content://com.android.externalstorage.documents/document/primary%3ADownload
> %2Fbankdrc.dbf from pid=15086, uid=10502 requires that you obtain access
> using ACTION_OPEN_DOCUMENT or related APIs
>
> There is a lot of boiler plate to handle the permissions to write to the
> system.
>
> Is there any example where this boiler plate code can be found?
>
Hi, I don't know any official example. The last Android-related point on the
QFile documentation suggests that Qt might not be of much help when it comes
to consuming content URLs (see https://doc.qt.io/qt-6/qfile.html).
I once wrote an app that is able to read and write content URLs using standard
I/O streams. You can find it here: https://github.com/Martchus/passwordmanager
It was written before Qt had support for Android file dialogs so I did that
part manually (but now you should of course rely on Qt for that). You could
probably also just use the QFile overload that takes a file descriptor instead
of using standard I/O streams because the latter is actually very involved. (I
only did it because my code was already using I/O streams.)
Another approach is to ditch the weird content URLs and work with normal
paths. For this you need to "convert" the content URL to a normal path. You
can find (probably very hacky) code on another project I'm currently working
on:
https://github.com/Martchus/syncthingtray/blob/master/tray/android/src/io/
github/martchus/syncthingtray/Util.java
Here I chose this approach because I also wanted to use std::filesystem and Go
code so this content stuff just gets in the way. I have actually just tested
whether this works on a real device and it does on an Android 14 one - even
writing to the SD card. You will most likely run into permission errors under
older Android versions. Not sure as of what version this approach works. I
think this approach also relies much on super greedy permissions (see https://
github.com/Martchus/syncthingtray/blob/
49e898644b8c1f4bdbcef2a2ddb8f4fcd410bdef/tray/resources/
AndroidManifest.xml.in#L11C55-L11C78) so that would be problematic if you
wanted to publish the app on Google Play.
Best Regards
Marius
More information about the Interest
mailing list