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.8k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on 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 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
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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 last edited by
          #5

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

          mrjjM 1 Reply Last reply
          0
          • M Mikeeeeee

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @mikeeeeee
            and how do you set downloadFolderAddress ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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

                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.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on 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 last edited by
                  #9

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

                  J.HilkJ 1 Reply Last reply
                  0
                  • M Mikeeeeee

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

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on 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 last edited by
                      #11
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mikeeeeee
                        wrote on 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.HilkJ 1 Reply Last reply
                        0
                        • M Mikeeeeee

                          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.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on 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
                          4
                          • J.HilkJ J.Hilk

                            @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 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