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 save files in Android in downloads?
Forum Updated to NodeBB v4.3 + New Features

How to save files in Android in downloads?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
14 Posts 5 Posters 4.6k Views 2 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on 28 Jul 2019, 15:35 last edited by Mikeeeeee
    #1

    Hi!
    How to save files in Android in downloads folder?
    My code saves in Windows, but it seems for Android me need to somehow specify the path to the folder.
    QFile *file = new QFile(downloadFolderAddress + nameFile);
    if(file->open(QFile::WriteOnly)){
    file->write(reply->readAll());
    file->close();

    J 1 Reply Last reply 29 Jul 2019, 05:38
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 28 Jul 2019, 15:47 last edited by
      #2

      Hi
      Check out
      https://doc.qt.io/qt-5/qstandardpaths.html
      and the DownloadLocation

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Mikeeeeee
        wrote on 28 Jul 2019, 18:21 last edited by
        #3

        So I found the download folders. But how to write the file? The file is not written by conventional means.
        qDebug()<<"Downloads folder: "<<QStandardPaths::standardLocations(QStandardPaths::DownloadLocation);
        Downloads folder: ("/storage/emulated/0/Download", "/storage/emulated/0/Android/data/org.qtproject.example.test/files/Download")

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 28 Jul 2019, 18:24 last edited by
          #4

          Hi
          its a list so it has 2 locations.
          You should use one of them as path.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mikeeeeee
            wrote on 28 Jul 2019, 18:28 last edited by
            #5

            Stops working before opening the file after
            QFile *file = new QFile(downloadFolderAddress + nameFile);

            M 1 Reply Last reply 28 Jul 2019, 18:39
            0
            • M Mikeeeeee
              28 Jul 2019, 18:28

              Stops working before opening the file after
              QFile *file = new QFile(downloadFolderAddress + nameFile);

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 28 Jul 2019, 18:39 last edited by
              #6

              @mikeeeeee
              and how do you set downloadFolderAddress ?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 28 Jul 2019, 20:30 last edited by
                #7

                Hi,

                To add to @mrjj, check that the folder you want to use exists and if not, create it. The locations retuned are valid but might not exist because there's not reason for them to have been created because your application might not use them at all.

                And unless nameFile starts with a slash, your path is going to point to something invalid.

                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
                3
                • M Mikeeeeee
                  28 Jul 2019, 15:35

                  Hi!
                  How to save files in Android in downloads folder?
                  My code saves in Windows, but it seems for Android me need to somehow specify the path to the folder.
                  QFile *file = new QFile(downloadFolderAddress + nameFile);
                  if(file->open(QFile::WriteOnly)){
                  file->write(reply->readAll());
                  file->close();

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 29 Jul 2019, 05:38 last edited by
                  #8

                  @mikeeeeee
                  to add to the others, the Download location on Android is outside of the sandbox of your app, so you'll need to WRITE_EXTERNAL_STORAGE inside your manifest and eventually request/check permission before your write attempt


                  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
                  3
                  • M Offline
                    M Offline
                    Mikeeeeee
                    wrote on 29 Jul 2019, 06:31 last edited by
                    #9

                    I added _EXTERNAL_STORAGE. How to query/check the permission before trying to write to?

                    J 1 Reply Last reply 29 Jul 2019, 06:35
                    0
                    • M Mikeeeeee
                      29 Jul 2019, 06:31

                      I added _EXTERNAL_STORAGE. How to query/check the permission before trying to write to?

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 29 Jul 2019, 06:35 last edited by
                      #10

                      @mikeeeeee

                      Check:
                      https://doc.qt.io/qt-5/qtandroid.html#checkPermission

                      request:
                      https://doc.qt.io/qt-5/qtandroid.html#requestPermissions
                      or
                      https://doc.qt.io/qt-5/qtandroid.html#requestPermissionsSync


                      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
                      2
                      • M Offline
                        M Offline
                        Mikeeeeee
                        wrote on 29 Jul 2019, 06:35 last edited by
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Mikeeeeee
                          wrote on 29 Jul 2019, 07:10 last edited by
                          #12

                          How to use? Do you have an example? In this code I get this error:
                          'QtAndroid::PermissionResult' is not contextually convertible to 'bool'
                          if (QtAndroid:: checkPermission("WRITE_EXTERNAL_STORAGE")) {qDebug()<<"good";} else {"bad";}

                          J 1 Reply Last reply 29 Jul 2019, 07:12
                          0
                          • M Mikeeeeee
                            29 Jul 2019, 07:10

                            How to use? Do you have an example? In this code I get this error:
                            'QtAndroid::PermissionResult' is not contextually convertible to 'bool'
                            if (QtAndroid:: checkPermission("WRITE_EXTERNAL_STORAGE")) {qDebug()<<"good";} else {"bad";}

                            J Offline
                            J Offline
                            J.Hilk
                            Moderators
                            wrote on 29 Jul 2019, 07:12 last edited by
                            #13

                            @mikeeeeee

                            bool requestAndroidPermissions(){
                            //Request requiered permissions at runtime

                            const QVector<QString> permissions({
                                                                "android.permission.WRITE_EXTERNAL_STORAGE",
                                                                "android.permission.READ_EXTERNAL_STORAGE"});
                            
                            for(const QString &permission : permissions){
                                auto result = QtAndroid::checkPermission(permission);
                                if(result == QtAndroid::PermissionResult::Denied){
                                    auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission}));
                                    if(resultHash[permission] == QtAndroid::PermissionResult::Denied)
                                        return false;
                                }
                            }
                            
                            return true;
                            

                            }


                            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.

                            J 1 Reply Last reply 12 May 2020, 10:16
                            4
                            • J J.Hilk
                              29 Jul 2019, 07:12

                              @mikeeeeee

                              bool requestAndroidPermissions(){
                              //Request requiered permissions at runtime

                              const QVector<QString> permissions({
                                                                  "android.permission.WRITE_EXTERNAL_STORAGE",
                                                                  "android.permission.READ_EXTERNAL_STORAGE"});
                              
                              for(const QString &permission : permissions){
                                  auto result = QtAndroid::checkPermission(permission);
                                  if(result == QtAndroid::PermissionResult::Denied){
                                      auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission}));
                                      if(resultHash[permission] == QtAndroid::PermissionResult::Denied)
                                          return false;
                                  }
                              }
                              
                              return true;
                              

                              }

                              J Offline
                              J Offline
                              jnpStimio
                              wrote on 12 May 2020, 10:16 last edited by
                              #14

                              @J-Hilk said in How to save files in Android in downloads?:

                              @mikeeeeee

                              bool requestAndroidPermissions(){
                              //Request requiered permissions at runtime

                              const QVector<QString> permissions({
                                                                  "android.permission.WRITE_EXTERNAL_STORAGE",
                                                                  "android.permission.READ_EXTERNAL_STORAGE"});
                              
                              for(const QString &permission : permissions){
                                  auto result = QtAndroid::checkPermission(permission);
                                  if(result == QtAndroid::PermissionResult::Denied){
                                      auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission}));
                                      if(resultHash[permission] == QtAndroid::PermissionResult::Denied)
                                          return false;
                                  }
                              }
                              
                              return true;
                              

                              }

                              You are wright !
                              I confirm you have to check permissions before writing file, EVEN if you have already asked permissions at the start of your app.

                              1 Reply Last reply
                              1

                              • Login

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