Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to open file with default application on Android (SDK 23 and above) using QDesktopServices

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

Scheduled Pinned Locked Moved Solved Mobile and Embedded
4 Posts 3 Posters 959 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.
  • D Offline
    D Offline
    Devoo
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Devoo
      wrote on last edited by
      #2

      I found solution.
      https://developer.android.com/reference/android/support/v4/content/FileProvider

      M 1 Reply Last reply
      1
      • D Devoo

        I found solution.
        https://developer.android.com/reference/android/support/v4/content/FileProvider

        M Offline
        M Offline
        mvuori
        wrote on last edited by
        #3

        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) {}
                }
            }
        
        R 1 Reply Last reply
        0
        • M mvuori

          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) {}
                  }
              }
          
          R Offline
          R Offline
          Rizni
          wrote on last edited by
          #4

          @mvuori Where did you wrote this code can you guide me please, I'm new to Qt andriod

          1 Reply Last reply
          0

          • Login

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