From 03d1d5bb8c381adb38bcda978209e7eb8f42efd9 Mon Sep 17 00:00:00 2001 From: Tomas Mizera Date: Thu, 5 Oct 2023 12:12:25 +0200 Subject: [PATCH] Use new storage permissions on Android >13 --- app/androidutils.cpp | 14 +++++++++++++- app/androidutils.h | 2 ++ cmake_templates/AndroidManifest.xml.in | 9 ++++----- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/androidutils.cpp b/app/androidutils.cpp index 2adaddc1c..98220f628 100644 --- a/app/androidutils.cpp +++ b/app/androidutils.cpp @@ -157,7 +157,19 @@ void AndroidUtils::quitApp() bool AndroidUtils::requestStoragePermission() { #ifdef ANDROID - if ( !checkAndAcquirePermissions( "android.permission.READ_EXTERNAL_STORAGE" ) ) + double buildVersion = QSysInfo::productVersion().toDouble(); + + // + // Android SDK 33 has a new set of permissions when reading external storage. + // See https://developer.android.com/reference/android/Manifest.permission#READ_EXTERNAL_STORAGE + // + QString storagePermissionType = QStringLiteral( "android.permission.READ_MEDIA_IMAGES" ); + if ( buildVersion < ANDROID_VERSION_13 ) + { + storagePermissionType = QStringLiteral( "android.permission.READ_EXTERNAL_STORAGE" ); + } + + if ( !checkAndAcquirePermissions( storagePermissionType ) ) { auto activity = QJniObject( QNativeInterface::QAndroidApplication::context() ); jboolean res = activity.callMethod( "shouldShowRequestPermissionRationale", "(Ljava/lang/String;)Z", QJniObject::fromString( "android.permission.WRITE_EXTERNAL_STORAGE" ).object() ); diff --git a/app/androidutils.h b/app/androidutils.h index 39f8c3d2b..e6d83f8c5 100644 --- a/app/androidutils.h +++ b/app/androidutils.h @@ -70,6 +70,8 @@ class AndroidUtils: public QObject const static int INSTALL_QR_SCANNER_CODE = 104; const static int QR_SCAN_CODE = 105; + const static int ANDROID_VERSION_13 = 13; + void handleActivityResult( int receiverRequestCode, int resultCode, const QJniObject &data ) override; #endif diff --git a/cmake_templates/AndroidManifest.xml.in b/cmake_templates/AndroidManifest.xml.in index 09f3f92a8..508360e23 100644 --- a/cmake_templates/AndroidManifest.xml.in +++ b/cmake_templates/AndroidManifest.xml.in @@ -5,14 +5,15 @@ android:versionCode="@INPUT_VERSION_CODE@" android:versionName="@INPUT_VERSION@"> - + + - - + + @@ -31,13 +32,11 @@ android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" /> -