I have a question about Android file permissions in QT
-
wrote on 5 Dec 2023, 04:42 last edited by
Now, I am trying to make android mp3 application using qt
I wrote source code below.
QString path1 = "/storage/emulated/0"; QDirIterator it(path1, QStringList() << "*.mp3", QDir::Files, QDirIterator::Subdirectories); while (it.hasNext()) { it.next(); listWidget->addItem(it.fileName()); }
but It did not work. So I think that I need the file Permissions.
I tried what I can everythings.
I use source code below. It worked before one years ago but It did not work now.
bool checkPermission() { auto r= QtAndroidPrivate::checkPermission(QStringLiteral("android.permission.READ_EXTERNAL_STORAGE")).result(); if (r == QtAndroidPrivate::Denied) { r = QtAndroidPrivate::checkPermission(QStringLiteral("android.permission.READ_EXTERNAL_STORAGE")).result(); if (r == QtAndroidPrivate::Denied) return false; } return true; }
*QStringLiteral("android.permission.READ_EXTERNAL_STORAGE") this part was QtAnroidPrivate::Storage. I edited my source code becuase QtAndroidPrivate::Storage is deprecated.
After a lot of research, I found an article that says that from Android 13 onwards, users doesn't need to request file permissions.
How to access external storage(sdcard/usb) in an android device in a qt qml based application?
and
QFileDialog::getOpenFileName(this, tr("Open File"), QDir::rootPath(), tr("Music (*.mp3)"));
The above source code works fine without file permissions.
I have a question, I would like to know why the code I wrote does not work.
QString path1 = "/storage/emulated/0"; QDirIterator it(path1, QStringList() << "*.mp3", QDir::Files, QDirIterator::Subdirectories); while (it.hasNext()) { it.next(); listWidget->addItem(it.fileName()); }
Please give me a hint. Thanks
1/1