1From 9de297110d3e18c94f278c9a98edc39e1d15ef8f Mon Sep 17 00:00:00 2001 2From: Sharaf Zaman <sharafzaz121@gmail.com> 3Date: Mon, 9 Nov 2020 08:40:57 +0000 4Subject: [PATCH] Android: use EXTRA_INITIAL_URI as the defaultDir 5 6on API Level >= 26, Storage Access Framework attempts to use the URI to 7save a new file at its parent location (i.e the parent directory). 8--- 9 .../qandroidplatformfiledialoghelper.cpp | 20 +++++++++++++++++++ 10 .../qandroidplatformfiledialoghelper.h | 4 +++- 11 2 files changed, 23 insertions(+), 1 deletion(-) 12 13diff --git a/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp b/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp 14index 05e4ccbbee..00b5b0887c 100644 15--- a/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp 16+++ b/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp 17@@ -61,6 +61,11 @@ QAndroidPlatformFileDialogHelper::QAndroidPlatformFileDialogHelper() 18 { 19 } 20 21+void QAndroidPlatformFileDialogHelper::setDirectory(const QUrl &directory) 22+{ 23+ m_directory = directory; 24+} 25+ 26 bool QAndroidPlatformFileDialogHelper::handleActivityResult(jint requestCode, jint resultCode, jobject data) 27 { 28 if (requestCode != REQUEST_CODE) 29@@ -125,6 +130,20 @@ void QAndroidPlatformFileDialogHelper::setIntentTitle(const QString &title) 30 extraTitle.object(), QJNIObjectPrivate::fromString(title).object()); 31 } 32 33+void QAndroidPlatformFileDialogHelper::setInitialUri() 34+{ 35+ if (QtAndroidPrivate::androidSdkVersion() >= 26) { 36+ const QJNIObjectPrivate extraInitialUri = QJNIObjectPrivate::getStaticObjectField( 37+ "android/provider/DocumentsContract", 38+ "EXTRA_INITIAL_URI", 39+ "Ljava/lang/String;"); 40+ m_intent.callObjectMethod("putExtra", 41+ "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;", 42+ extraInitialUri.object(), 43+ QJNIObjectPrivate::fromString(m_directory.toString()).object()); 44+ } 45+} 46+ 47 void QAndroidPlatformFileDialogHelper::setOpenableCategory() 48 { 49 const QJNIObjectPrivate CATEGORY_OPENABLE = QJNIObjectPrivate::getStaticObjectField( 50@@ -229,6 +248,7 @@ bool QAndroidPlatformFileDialogHelper::show(Qt::WindowFlags windowFlags, Qt::Win 51 } 52 53 setIntentTitle(options()->windowTitle()); 54+ setInitialUri(); 55 56 QtAndroidPrivate::registerActivityResultListener(this); 57 m_activity.callMethod<void>("startActivityForResult", "(Landroid/content/Intent;I)V", 58diff --git a/src/plugins/platforms/android/qandroidplatformfiledialoghelper.h b/src/plugins/platforms/android/qandroidplatformfiledialoghelper.h 59index 5a7a28a8a0..9a2e037e6f 100644 60--- a/src/plugins/platforms/android/qandroidplatformfiledialoghelper.h 61+++ b/src/plugins/platforms/android/qandroidplatformfiledialoghelper.h 62@@ -68,7 +68,7 @@ public: 63 QList<QUrl> selectedFiles() const override { return m_selectedFile; }; 64 void selectFile(const QUrl &file) override { Q_UNUSED(file) }; 65 QUrl directory() const override { return QUrl(); }; 66- void setDirectory(const QUrl &directory) override { Q_UNUSED(directory) }; 67+ void setDirectory(const QUrl &directory) override; 68 bool defaultNameFilterDisables() const override { return false; }; 69 bool handleActivityResult(jint requestCode, jint resultCode, jobject data) override; 70 71@@ -76,6 +76,7 @@ private: 72 QJNIObjectPrivate getFileDialogIntent(const QString &intentType); 73 void takePersistableUriPermission(const QJNIObjectPrivate &uri); 74 void setIntentTitle(const QString &title); 75+ void setInitialUri(); 76 void setOpenableCategory(); 77 void setAllowMultipleSelections(bool allowMultiple); 78 void setMimeTypes(); 79@@ -84,6 +85,7 @@ private: 80 QList<QUrl> m_selectedFile; 81 QJNIObjectPrivate m_intent; 82 const QJNIObjectPrivate m_activity; 83+ QUrl m_directory; 84 }; 85 86 } 87-- 882.28.0 89 90