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. Access Documents/Pictures folder on Android
Forum Updated to NodeBB v4.3 + New Features

Access Documents/Pictures folder on Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
4 Posts 2 Posters 1.4k 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.
  • H Offline
    H Offline
    Holger Gerth
    wrote on last edited by Holger Gerth
    #1

    I'm trying to access and list the files in the Document/Pictures folder on Android via QDir::entryInfoList but no content is found:

    QStringList paths = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
    
    foreach (auto path, paths) {
      qDebug() << "path: " << path;
      QDir directory(path);
      qDebug() << "EntryList: " << directory.entryList();
      qDebug() << "FileInfoList: " <<  directory.entryInfoList();
    }
    

    On desktop, this code is working find, delivering the content of the users pictures folder. On Android I get these results:

    D MyApp: path:  "/storage/emulated/0/Pictures"
    D MyAppr: EntryList:  ()
    D MyApp: FileInfoList:  ()
    D MyApp: path:  "/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures"
    D MyApp: EntryList:  (".", "..")
    D MyApp: FileInfoList:  (QFileInfo(/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures/.), QFileInfo(/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures/..))
    

    But there are files in the pictures folder of the device. And the path should be correct.

    Read external storage permission is set in the AndroidManifest.xml.

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    

    Any ideas what I'm doing wrong? Or is this a bug in QDir?

    KroMignonK 1 Reply Last reply
    0
    • H Holger Gerth

      I'm trying to access and list the files in the Document/Pictures folder on Android via QDir::entryInfoList but no content is found:

      QStringList paths = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
      
      foreach (auto path, paths) {
        qDebug() << "path: " << path;
        QDir directory(path);
        qDebug() << "EntryList: " << directory.entryList();
        qDebug() << "FileInfoList: " <<  directory.entryInfoList();
      }
      

      On desktop, this code is working find, delivering the content of the users pictures folder. On Android I get these results:

      D MyApp: path:  "/storage/emulated/0/Pictures"
      D MyAppr: EntryList:  ()
      D MyApp: FileInfoList:  ()
      D MyApp: path:  "/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures"
      D MyApp: EntryList:  (".", "..")
      D MyApp: FileInfoList:  (QFileInfo(/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures/.), QFileInfo(/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures/..))
      

      But there are files in the pictures folder of the device. And the path should be correct.

      Read external storage permission is set in the AndroidManifest.xml.

      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
      

      Any ideas what I'm doing wrong? Or is this a bug in QDir?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #2

      @Holger-Gerth Welcome to Qt Forum, which Qt version are you using and with Android version do you targeting?

      Have you checked READ_EXTERNAL_STORAGE permission is granted to your app?

      You could do it with something like this:

      bool checkPermissions()
      {
          bool success = true;
          if(QtAndroid::androidSdkVersion() >= 23)
          {
              static const QVector<QString> permissions({
                  "android.permission.READ_EXTERNAL_STORAGE"
              });
      
              for(const QString &permission : permissions)
              {
                  // check if permission is granded
                  auto result = QtAndroid::checkPermission(permission);
                  if(result != QtAndroid::PermissionResult::Granted)
                  {
                      // request permission
                      auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission}));
                      if(resultHash[permission] != QtAndroid::PermissionResult::Granted)
                      {
                          qDebug() << "Fail to get permission" << permission;
                          success = false;
                      }
                      else
                      {
                          qDebug() << "Permission" << permission << "granted!";
                      }
                  }
                  else
                  {
                      qDebug() << "Permission" << permission << "already granted!";
                  }
              }
          }
          return success;
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      1
      • H Offline
        H Offline
        Holger Gerth
        wrote on last edited by
        #3

        Android target sdk level is 28,
        Qt version is 5.15.0

        That was the solution, thank you. I wasn't aware that I have to explicitly check for the permission. I thought that this will be done on installation/startup time via the manifest.

        KroMignonK 1 Reply Last reply
        0
        • H Holger Gerth

          Android target sdk level is 28,
          Qt version is 5.15.0

          That was the solution, thank you. I wasn't aware that I have to explicitly check for the permission. I thought that this will be done on installation/startup time via the manifest.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @Holger-Gerth said in Access Documents/Pictures folder on Android:

          That was the solution, thank you. I wasn't aware that I have to explicitly check for the permission. I thought that this will be done on installation/startup time via the manifest.

          Your welcome :)
          Permission check policy changes with each new Android API level, so it depends which is the Android device you are targeting.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          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