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"


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



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


  • Moderators

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



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


Log in to reply
 

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