Access Documents/Pictures folder on Android
-
I'm trying to access and list the files in the Document/Pictures folder on Android via QDir::entryInfoList but no content is found:
QStringList paths = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation); foreach (auto path, paths) { qDebug() << "path: " << path; QDir directory(path); qDebug() << "EntryList: " << directory.entryList(); qDebug() << "FileInfoList: " << directory.entryInfoList(); }
On desktop, this code is working find, delivering the content of the users pictures folder. On Android I get these results:
D MyApp: path: "/storage/emulated/0/Pictures" D MyAppr: EntryList: () D MyApp: FileInfoList: () D MyApp: path: "/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures" D MyApp: EntryList: (".", "..") D MyApp: FileInfoList: (QFileInfo(/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures/.), QFileInfo(/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures/..))
But there are files in the pictures folder of the device. And the path should be correct.
Read external storage permission is set in the AndroidManifest.xml.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Any ideas what I'm doing wrong? Or is this a bug in QDir?
-
I'm trying to access and list the files in the Document/Pictures folder on Android via QDir::entryInfoList but no content is found:
QStringList paths = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation); foreach (auto path, paths) { qDebug() << "path: " << path; QDir directory(path); qDebug() << "EntryList: " << directory.entryList(); qDebug() << "FileInfoList: " << directory.entryInfoList(); }
On desktop, this code is working find, delivering the content of the users pictures folder. On Android I get these results:
D MyApp: path: "/storage/emulated/0/Pictures" D MyAppr: EntryList: () D MyApp: FileInfoList: () D MyApp: path: "/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures" D MyApp: EntryList: (".", "..") D MyApp: FileInfoList: (QFileInfo(/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures/.), QFileInfo(/storage/emulated/0/Android/data/de.myorg.myapp/files/Pictures/..))
But there are files in the pictures folder of the device. And the path should be correct.
Read external storage permission is set in the AndroidManifest.xml.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Any ideas what I'm doing wrong? Or is this a bug in QDir?
@Holger-Gerth Welcome to Qt Forum, which Qt version are you using and with Android version do you targeting?
Have you checked READ_EXTERNAL_STORAGE permission is granted to your app?
You could do it with something like this:
bool checkPermissions() { bool success = true; if(QtAndroid::androidSdkVersion() >= 23) { static const QVector<QString> permissions({ "android.permission.READ_EXTERNAL_STORAGE" }); for(const QString &permission : permissions) { // check if permission is granded auto result = QtAndroid::checkPermission(permission); if(result != QtAndroid::PermissionResult::Granted) { // request permission auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission})); if(resultHash[permission] != QtAndroid::PermissionResult::Granted) { qDebug() << "Fail to get permission" << permission; success = false; } else { qDebug() << "Permission" << permission << "granted!"; } } else { qDebug() << "Permission" << permission << "already granted!"; } } } return success; }
-
Android target sdk level is 28,
Qt version is 5.15.0That was the solution, thank you. I wasn't aware that I have to explicitly check for the permission. I thought that this will be done on installation/startup time via the manifest.
-
Android target sdk level is 28,
Qt version is 5.15.0That was the solution, thank you. I wasn't aware that I have to explicitly check for the permission. I thought that this will be done on installation/startup time via the manifest.
@Holger-Gerth said in Access Documents/Pictures folder on Android:
That was the solution, thank you. I wasn't aware that I have to explicitly check for the permission. I thought that this will be done on installation/startup time via the manifest.
Your welcome :)
Permission check policy changes with each new Android API level, so it depends which is the Android device you are targeting.