Qt 6, Android 11 widget application, read and write to file. permission denied

539 Views Asked by At

hope can point me in the right direction on how to fix my application so i could read and write a simple .txt file.

i think i have tried all solutions out thier is on the world wide web, but hit me with what i should try.

things i have tried:

    bool value = QJniObject::callStaticMethod<jboolean>("android/os/Environment","isExternalStorageManager");
    if(!value){
        QJniObject filepermit = QJniObject::getStaticObjectField("android/provider/Settings","ACTION_MANAGE_APP_ALL_FILES_ACCES_PERMISSION","Ljava/lang/string;");
        //you must changing the YOURPKGNAME at below
        QJniObject pkgName = QJniObject::fromString("package:org.qtproject.example.robotinterfaceandroidqt");
        QJniObject parsedUri = QJniObject::callStaticObjectMethod(
                        "android/net/Uri",
                        "parse","(Ljava/lang/String;)Landroid/net/Uri;",
                         pkgName.object<jstring>());
        QJniObject intent("android/content/Intent",
                          "(Ljava/lang/String;Landroid/net/Uri;)V",
                          filepermit.object<jstring>(),parsedUri.object());
        QtAndroidPrivate::startActivity(intent, 0);
    }else{
        qDebug() << "NEW PERMISSION IS NOT GRANTED";
    }

this gives me a prompt screen to allow acces but in allow acces to all files it is just all gray and cant be set to permission to acces all files.

The other thing i have tried:

int counter = 0;
bool permission(){
    auto result = QtAndroidPrivate::checkPermission("android.permission.MANAGE_EXTERNAL_STORAGE").result();
    if(result == QtAndroidPrivate::PermissionResult::Denied){
        result = QtAndroidPrivate::requestPermission(QString("android.permission.MANAGE_EXTERNAL_STORAGE")).result();
        counter++;
        if(counter > 10)
            qDebug() << "CANNOT GIVE THAT KIND OF PERMISSION";
        permission();
    }else{
        counter = 0;
        return true;
    }
return false;
}

have also tryied that with EXTERNAL_READ_STORAGE and write even with qts own QtAndroidPrivate::Storage with out any luck.

i am on sdk 31 building app towards android 11 using qt, cmake,

UPDATE and clarify.

in my app i try and create a txt file to save JSON data it create a file on the device but i cannot open and write to the file. the error it gives is

<E Qt JAVA : openParcelFdForContentUrl: java.lang.SecurityException: Permission Denial: writing com.android.providers.downloads.DownloadStorageProvider uri content://com.android.providers.downloads.documents/document/62.txt from pid=2455, uid=10179 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()>

and i have tried with the following permission request:

auto result = QtAndroidPrivate::checkPermission("android.permission.MANAGE_DOCUMENTS").result();
if(result == QtAndroidPrivate::PermissionResult::Denied){
    result = QtAndroidPrivate::requestPermission(QString("android.permission.MANAGE_DOCUMENTS")).result();
  }

any ideas ?

the whole file thingy:

 QString  filename = QFileDialog::getSaveFileName();
filename = filename + ".txt";
qDebug() << "the file name is : " + filename;
ui->startPageProgramName->setText(test.toString());
auto result = QtAndroidPrivate::checkPermission("android.permission.MANAGE_DOCUMENTS").result();
if(result == QtAndroidPrivate::PermissionResult::Denied){
    result = QtAndroidPrivate::requestPermission(QString("android.permission.MANAGE_DOCUMENTS")).result();
  }
QFile file(filename);
if(!file.open(QIODevice::WriteOnly)){
    qDebug() << file.errorString();
    return;
}else{
QTextStream out(&file);
out << "QString::fromStdString(mainPro_->programToString())";
qDebug() << &out;
out.flush();
out << "testing som more";
out << "agian";
file.close();
}
0

There are 0 best solutions below