Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-97779

QFile and QDir do not work on Android 11 (SDK level 30)

    XMLWordPrintable

Details

    • Bug
    • Resolution: Incomplete
    • P1: Critical
    • None
    • 5.15.1, 5.15.6, 5.15.7
    • Core: I/O
    • Android 11
    • Android
    • 2022wk02FOAndroid&Automotive, 2022wk04FOAndroid&Automotive

    Description

      It appears that due to Android 11's changes to storage permissions, this issue (https://bugreports.qt.io/browse/QTBUG-85538?gerritReviewStatus=All) has become relevant again.  But because this issue is slightly different compared to that one, a new card is necessary.

       

      In Android 11, when using the system file picker the user manually authorizes access to the selected file, or directory. But after doing so (using FileDialog, and converting the content URL to a file path), QDir and QFile are unable to access the files selected.

      I have created an example android app which you can find below that replicates this issue on a smaller scale. You select either a folder or file, and it lists the directory's entire contents, or reads the file's contents, respectively.

       

      The tests:

      • Samsung S9 (Android 8)
        • Selected a custom folder (tested under /sdcard, /sdcard/Downloads, /sdcard/my_folder) containing some files and sub-folders
          • Result: All files and folder names are discovered by directory.entryList(QDir::AllEntries) and displayed
        • Selected a text file.
          • Result: QFile is able to open the file and reads its contents
      • Samsung S7+ (Android 11)
        • Selected a custom folder (tested under /sdcard, /sdcard/Downloads, /sdcard/my_folder) containing some files and sub-folders
          • Result: directory.entryList(QDir::AllEntries) only discovers folders, and not files
        • Selected a text file.
          • Result: QFile is unable to open the file

      READ_EXTERNAL_STORAGE is set both in the manifest and runtime.
      I have also created a basic helper class with some Q_INVOKABLE functions that act as entries for QDir's entryList() and QFile's readAll() under FileDirHelper.h on the QML side.

       

      A temporary workaround is to pass the URL to Java code and open an inputstream using a ContentResolver for reading files.

       

      Lastly, for whatever reason when I tried to build this on Qt Creator for Android 11, despite specifying the target sdk version in the manifest it kept defaulting to 28. So you'll need to modify the build.gradle when running it for android 11 to:
      compileSdkVersion 30
      ...
      defaultConfig

      { minSdkVersion = 30 targetSdkVersion = 30 }

      Attachments

        1. android8example.PNG
          android8example.PNG
          51 kB
        2. example_test.zip
          63 kB
        3. qdirfailed.PNG
          qdirfailed.PNG
          63 kB
        4. readfailed.PNG
          readfailed.PNG
          56 kB
        5. Screenshot_20220105-103419.jpg
          Screenshot_20220105-103419.jpg
          431 kB

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              assam Assam Boudjelthia
              tacotuesdaysattacobell John Smith
              Votes:
              3 Vote for this issue
              Watchers:
              12 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes