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. Can't save file to external storage on android
QtWS25 Last Chance

Can't save file to external storage on android

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
11 Posts 9 Posters 7.0k 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.
  • I Offline
    I Offline
    ifhmcos
    wrote on 17 Jan 2018, 13:23 last edited by ifhmcos
    #1

    I'm trying to write to an external SdCard with QFile, running Qt 5.10 and android 5.1. And I get a permission denied error. It is working with the internal storage.

    #include <QCoreApplication>    
    #include <QFile>
    #include <QTextStream>
    #include <QDebug>
    
    #ifdef __ANDROID__
    #include <QtAndroid>
    #endif
    
    bool check_permission() {
        QtAndroid::PermissionResult r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
        if(r == QtAndroid::PermissionResult::Denied) {
            QtAndroid::requestPermissionsSync( QStringList() << "android.permission.WRITE_EXTERNAL_STORAGE" );
            r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
            if(r == QtAndroid::PermissionResult::Denied) {
                qDebug() << "Permission denied";
                return false;
            }
        }
    
        qDebug() << "Permissions granted!";
        return true;
    }
    
    int main(int argc, char *argv[]) {
        QCoreApplication a(argc, argv);
    
        if (!check_permission())
            return -1;
    
        QFile f("/storage/extSdCard/WriteTest.txt");
        if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) {
            qDebug() << "Error while opening file to save";
            qDebug() << f.errorString();
            return -1;
        }
    
        QTextStream out(&f);
        out << "This is just a test\n";
    
        return a.exec();
    }
    

    whhere check_permission is a function I took from Qt's bugtracker that needs to be used with Qt 5.10 > and android SDK > 22. The thing is I'm at android SDK < 22.

    here is the function just in case. I stepped into it and it returns true on checkPermission (the external storage write flag is set in the androidManifest too)

    And my output

    Permissions granted!
    Error while opening file to save
    "Permission denied"
    
    K 1 Reply Last reply 17 Jan 2018, 14:28
    1
    • I ifhmcos
      17 Jan 2018, 13:23

      I'm trying to write to an external SdCard with QFile, running Qt 5.10 and android 5.1. And I get a permission denied error. It is working with the internal storage.

      #include <QCoreApplication>    
      #include <QFile>
      #include <QTextStream>
      #include <QDebug>
      
      #ifdef __ANDROID__
      #include <QtAndroid>
      #endif
      
      bool check_permission() {
          QtAndroid::PermissionResult r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
          if(r == QtAndroid::PermissionResult::Denied) {
              QtAndroid::requestPermissionsSync( QStringList() << "android.permission.WRITE_EXTERNAL_STORAGE" );
              r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
              if(r == QtAndroid::PermissionResult::Denied) {
                  qDebug() << "Permission denied";
                  return false;
              }
          }
      
          qDebug() << "Permissions granted!";
          return true;
      }
      
      int main(int argc, char *argv[]) {
          QCoreApplication a(argc, argv);
      
          if (!check_permission())
              return -1;
      
          QFile f("/storage/extSdCard/WriteTest.txt");
          if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) {
              qDebug() << "Error while opening file to save";
              qDebug() << f.errorString();
              return -1;
          }
      
          QTextStream out(&f);
          out << "This is just a test\n";
      
          return a.exec();
      }
      

      whhere check_permission is a function I took from Qt's bugtracker that needs to be used with Qt 5.10 > and android SDK > 22. The thing is I'm at android SDK < 22.

      here is the function just in case. I stepped into it and it returns true on checkPermission (the external storage write flag is set in the androidManifest too)

      And my output

      Permissions granted!
      Error while opening file to save
      "Permission denied"
      
      K Offline
      K Offline
      koahnig
      wrote on 17 Jan 2018, 14:28 last edited by
      #2

      @ifhmcos

      I think you may have to give also permissions on the Android device itself for the application. At least what I found when adding an external SD card after the application had been installed already.

      I followed this video and had to use my imagination because my Android device is in German and it is also Android 7. However after granting rights in settings, I was ableto copy to external SD card.

      Hope it helps in your case.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 17 Jan 2018, 15:33 last edited by
        #3

        Hi,

        Are you sure the path is valid ?

        It might be related to the issue with 5.10.0 described here. Can you check whether it works with the latest 5.9 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • I Offline
          I Offline
          ifhmcos
          wrote on 17 Jan 2018, 15:47 last edited by ifhmcos
          #4

          @koahnig

          I have the permissions necessary in the android options

          @SGaist

          I was aware of that, tested with both 5.9 and 5.10.

          1 Reply Last reply
          1
          • D Offline
            D Offline
            Dabulla
            wrote on 17 Jan 2019, 11:39 last edited by
            #5

            I have the same problem (Qt5.12.0). Did you find a solution?

            I can write files to "/emulated/0/" but not to "/storage".
            Permissions are set, I integrated the permission-dialog and hit accept. App-Settings permission is set (WRITE_EXTERNAL_STORAGE).
            (READ_EXTERNAL_STORAGE is only set in Manifest.)

            I tried writing with QFile and in Qml with XMLHttpRequest's "PUT" method.
            I tried Urls with "file:///", "file://", "/" and "".

            I get "QIODevice::write (QFile, "/storage/3962-3732/[...]"): device not open.

            J.HilkJ 1 Reply Last reply 17 Jan 2019, 11:55
            0
            • D Dabulla
              17 Jan 2019, 11:39

              I have the same problem (Qt5.12.0). Did you find a solution?

              I can write files to "/emulated/0/" but not to "/storage".
              Permissions are set, I integrated the permission-dialog and hit accept. App-Settings permission is set (WRITE_EXTERNAL_STORAGE).
              (READ_EXTERNAL_STORAGE is only set in Manifest.)

              I tried writing with QFile and in Qml with XMLHttpRequest's "PUT" method.
              I tried Urls with "file:///", "file://", "/" and "".

              I get "QIODevice::write (QFile, "/storage/3962-3732/[...]"): device not open.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on 17 Jan 2019, 11:55 last edited by
              #6

              @Dabulla hi,
              is your android version >= 6 ?
              Because in the android documentation it says:

              In Android 6.0, any device that is not adopted is considered portable. Because portable storage is connected for only a short time, the platform avoids heavy operations such as media scanning. Third-party apps must go through the Storage Access Framework to interact with files on portable storage; direct access is explicitly blocked for privacy and security reasons.

              I don't think Qt's QFile goes natively through the Storage Access Framework . However, I may be wrong if someone knows more about it...


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • D Offline
                D Offline
                Dabulla
                wrote on 6 Feb 2019, 19:54 last edited by
                #7

                @J.Hilk thanks a lot for this hint. I just stopped investigating into this issue after your information :).

                I guess it is an advantage to know native Java Android development before using Qt for Android and running into issues like this.

                1 Reply Last reply
                0
                • ismailI Offline
                  ismailI Offline
                  ismail
                  wrote on 16 Apr 2019, 20:26 last edited by
                  #8

                  @Dabulla I solved same problem with this library:
                  https://github.com/FalsinSoft/QtAndroidTools

                  Also, you should read this document about permission problem:
                  https://falsinsoft.github.io/QtAndroidTools/Documentation/#AppPermissions

                  I hope it helps someone.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Afflospark
                    Banned
                    wrote on 17 Apr 2019, 12:49 last edited by
                    #9

                    Hello,
                    As I read you have an issue that you cant save the file in internal storage.
                    This issue coming due to you have given the internal storage path.
                    Please check you have given a right path for application to save files.
                    If you have done this process then you have to give all the permission to your Qt application
                    Then your file will be saved where you want.
                    Hope this will help you
                    Thanks

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mahdi.ze
                      wrote on 19 Jul 2022, 18:08 last edited by
                      #10

                      you should use android uri for the file in order to meet android storage access framework conditions.
                      when working with android uri you can use CrossQFile to work with the file.
                      https://github.com/mahdize/CrossQFile

                      https://developer.android.com/guide/topics/providers/document-provider

                      1 Reply Last reply
                      0
                      • SeDiS Offline
                        SeDiS Offline
                        SeDi
                        wrote on 29 Jul 2022, 19:13 last edited by
                        #11

                        Is sharing data what you actually want to achieve? Look here: https://github.com/ekke/ekkesSHAREexample

                        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