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. Qt 6.7 Android reading and opening files
Forum Updated to NodeBB v4.3 + New Features

Qt 6.7 Android reading and opening files

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
9 Posts 3 Posters 1.1k Views 1 Watching
  • 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.
  • 5 Offline
    5 Offline
    5asha
    wrote on last edited by
    #1

    I'm currently working on making a windows c++ program run on android. I've made it so it can save files correctly now, but the program isn't able to open or read content uris once I save and if I try to open a file. I'm wondering about the proper way to go about doing this, as I've noticed a lot of methods are outdated with new QT versions, and I've been going in circles and dead ends with my methods.

    Do I need a runtime permission manager? Or is changing the AndroidManifest.xml enough?

    Are java and jni classes necessary? Or is there another way to get this done?

    Any methods or help would be greatly appreciated. Thank you

    1 Reply Last reply
    0
    • mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote on last edited by
      #2

      Hi, the way to read and write files on Android is the same such as Windows or Linux.
      But pay attention: you can't read and write files where you want in Android.
      The old Android versions such as Android 5, permit to read and write data in Documents, Download and home dir folders. The latest versions not.

      Now I use this code line
      QDir Dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));

      to have a folder where I can read and write data.

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      2
      • I Offline
        I Offline
        imahgin
        wrote on last edited by
        #3

        You'll probably also need to deal with Android permissions

        1 Reply Last reply
        0
        • 5 Offline
          5 Offline
          5asha
          wrote on last edited by 5asha
          #4

          Do I need a java class or jni at all though to deal with the content uri? I have the manifest permissions and runtime permissions included.@mrdebug

          1 Reply Last reply
          0
          • mrdebugM Offline
            mrdebugM Offline
            mrdebug
            wrote on last edited by
            #5

            Where do you want to read or write the file?
            Which is your android version?

            Need programmers to hire?
            www.labcsp.com
            www.denisgottardello.it
            GMT+1
            Skype: mrdebug

            5 1 Reply Last reply
            0
            • mrdebugM mrdebug

              Where do you want to read or write the file?
              Which is your android version?

              5 Offline
              5 Offline
              5asha
              wrote on last edited by
              #6

              @mrdebug I am using Android 13 (API level 33) and I'm looking to save files in the documents, downloads, home dir folders that you had mentioned. Thank you

              1 Reply Last reply
              0
              • mrdebugM Offline
                mrdebugM Offline
                mrdebug
                wrote on last edited by
                #7

                I think Android 13 does not permit to access at home dir folders. I suggest you to try a file explorer example to verify which folders you can open.
                Pay attantion, the manifest file is not enought to require persmissions. Some recents android Versions want that the permissions must be request on startup of the app.

                        const QVector<QString> Permissions({"android.permission.ACCESS_COARSE_LOCATION",
                                                            "android.permission.ACCESS_FINE_LOCATION",
                                                            "android.permission.ACCESS_NETWORK_STATE",
                                                            "android.permission.ACCESS_WIFI_STATE",
                                                            "android.permission.INTERNET",
                                                            "android.permission.NFC",
                                                            "android.permission.READ_EXTERNAL_STORAGE",
                                                            "android.permission.WRITE_EXTERNAL_STORAGE"});
                        for (const QString &Permission : Permissions) {
                            switch(QtAndroid::checkPermission(Permission)) {
                                case QtAndroid::PermissionResult::Denied: {
                                    qDebug() << Permission+ " requested";
                                    QtAndroid::requestPermissionsSync(QStringList({Permission}));
                                    switch(QtAndroid::checkPermission(Permission)) {
                                        case QtAndroid::PermissionResult::Denied: qDebug() << Permission+ " denied"; break;
                                        case QtAndroid::PermissionResult::Granted: qDebug() << Permission+ " already granted"; break;
                                    }
                                    break;
                                }
                                case QtAndroid::PermissionResult::Granted: qDebug() << Permission+ " already granted"; break;
                            }
                        }
                

                Need programmers to hire?
                www.labcsp.com
                www.denisgottardello.it
                GMT+1
                Skype: mrdebug

                1 Reply Last reply
                0
                • 5 Offline
                  5 Offline
                  5asha
                  wrote on last edited by
                  #8

                  Thank you @mrdebug , I had the same except using QJniObject::callStaticMethod . I'm not sure if QtAndroid is available for 6.7 however? I tried looking into it and I may have to use QtAndroidPrivate using #include <QtCore/private/qandroidextras_p.h> for the permissions.

                  I am still wondering: do I need to use a java class in my project to deal with content uri? Thanks.

                  1 Reply Last reply
                  0
                  • mrdebugM Offline
                    mrdebugM Offline
                    mrdebug
                    wrote on last edited by mrdebug
                    #9

                    Hi, I'm still using Qt5 but I think Qt6 is the same.
                    I think it is non necessary to use java to read and write file or to relose an uri.
                    I think the Qt classes are enought.
                    I'm not so expert on Android. You can ask here
                    https://xdaforums.com

                    Need programmers to hire?
                    www.labcsp.com
                    www.denisgottardello.it
                    GMT+1
                    Skype: mrdebug

                    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