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. Qt failed to write external SD card on Android
Forum Updated to NodeBB v4.3 + New Features

Qt failed to write external SD card on Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
2 Posts 1 Posters 396 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.
  • A Offline
    A Offline
    Ader
    wrote on last edited by
    #1

    When using Qt on Android, I found that the external SD card can be read but cannot be written. I applied for write permission when using it.AndroidManifest.xml has set <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QtQuick/QQuickView>
    #include <QQmlContext>
    #include <QDebug>
    #include <QFile>
    #include <QtAndroid>

    bool checkPermission() {
    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");//READ_EXTERNAL_STORAGE
    if(r == QtAndroid::PermissionResult::Denied) {
    return false;
    }
    }
    return true;
    }

    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    checkPermission();

    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);
    
    QString string = findSDCardStr();
    QFile* file=new QFile(string + "/test.txt");
    if (file->exists()) {
        if(file->open(QIODevice::WriteOnly))
            qDebug()<<"true";
        else
           qDebug()<<"false";
    }
    return app.exec();
    

    }

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Ader
      wrote on last edited by
      #2

      I found that QtAndroid setting permissions on the external SD card did not work, so I used java, but the first time I put the file in the root directory, it also prompted the file to have no permissions until I saw this https://stackoverflow. com/questions/43349110/android-write-on-external-sd-card-java-io-ioexception-permission-denied found the new folder path, and then I tried it and it can be used

      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