Can't save file to external storage on android



  • 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"

  • Moderators

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


  • Lifetime Qt Champion

    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 ?



  • @koahnig

    I have the permissions necessary in the android options

    @SGaist

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.