Home > Software design >  Qt Android Directories
Qt Android Directories

Time:10-10

I am concerned about two questions regarding how Qt works with Android directories.

  1. For example, I want to download an archive via a library (QuaZip), but I need it to be downloaded along the path "/ storage / emulated / 0 / Android / data /", and then unzipped right there. But the problem is that the file is saved in the patch "/data/user/0/org.qtproject.example.NameApp". How can this be fixed? My code (it is unlikely that it works at all, but on Windows it would work for sure):
m_file.rename ("/storage/emulated/0/Android/data/cache.zip");
  1. Can I open a file in Android via QProccess?

CodePudding user response:

On Android 11 Android/data is forbidden for app access without root, see docs1 and docs2.

For read/write to other locations:

  1. The directory must exis or must be created (with QDir::mkpath() at once, or with QDir::mkdir() by subdirectories sequentally, because it can't create trees of directories).
  2. QFile must be closed before rename.
  3. App must have android.permission.READ_EXTERNAL_STORAGE and android.permission.WRITE_EXTERNAL_STORAGE permissions.
  4. At last, approve file access permissions manually in Android apps settings after app is deployed from Qt Creator. And you also may have to launch app manually, because there were reports about different app behavior when launched from IDE and and when launched manually from device.

This code worked for me (Android 11, Qt 5.15.2):

bool result;
QDir dir("/storage/emulated/0");
qDebug() << dir;
result = dir.mkdir("cache");
qDebug() << "mkdir(\"cache\") success: " << result;
QFile m_file(dir.path()   "/cache/cache.txt");
result = m_file.open(QFile::WriteOnly);
qDebug() << "open() success: " << result;
m_file.close();
result = m_file.rename(dir.path()   "/cache/cache.zip");
qDebug() << "rename() success: " << result;

I'll try to construct some example on second part of your question and update my answer a few days later.

  • Related