How to open file with default application on Android (SDK 23 and above) using QDesktopServices



  • Hi,
    I try to open pdf file with default application on Android (SDK 23 or above) using QDesktopServices like that (old code):

    QDesktopServices desk;
    desk.openUrl(QUrl("file:/storage/emulated/0/Download/test.pdf"));
    

    I know that file:// prefix is not allowed anymore.
    We should send the URI through content:// scheme instead.
    I don't understand how implement this feature.
    This code:

    QDesktopServices desk;
    desk.openUrl(QUrl("content:///storage/emulated/0/Download/test.pdf"));
    

    opens the default application, but the file is not loaded.

    Qt 5.13.1 (QFile works as expected).
    NDK 19c
    Android 9.0

    Help.





  • Setting up FileProvider seems a lot of unnecessary somewhat complicated (for people who visit the Java side every two years...) work for keeping old apps running. Today I ran into a piece of Java code that enables openUrl:

    public static void allowUriSchemes() {
            if (android.os.Build.VERSION.SDK_INT >= 23) {
                // Regular file:// is disabled
                try {
                    final Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                    m.invoke(null);
                } catch (final Exception e) {}
            }
        }
    

Log in to reply