Android: fix mimeTypes handling for file dialog
Since Android native file manager supports mimeTypes, I use QMimeDatabase to get the correct mime type for the nameFilter used with the file dialog. [ChangeLog][Android] Support setting mimetypes and and namefilters for Android native file dialog. Task-number: QTBUG-83089 Change-Id: I46545576dc9b51aa872bb37dbf4fe12b2533bdd9 Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>bb10
parent
540b6f6d64
commit
dbaf62033a
|
|
@ -157,6 +157,11 @@ public class QtNative
|
|||
}
|
||||
}
|
||||
|
||||
public static String[] getStringArray(String joinedString)
|
||||
{
|
||||
return joinedString.split(",");
|
||||
}
|
||||
|
||||
private static Uri getUriWithValidPermission(Context context, String uri, String openMode)
|
||||
{
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -42,6 +42,10 @@
|
|||
#include <androidjnimain.h>
|
||||
#include <jni.h>
|
||||
|
||||
#include <QMimeType>
|
||||
#include <QMimeDatabase>
|
||||
#include <QRegularExpression>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
namespace QtAndroidFileDialogHelper {
|
||||
|
|
@ -147,17 +151,47 @@ void QAndroidPlatformFileDialogHelper::setAllowMultipleSelections(bool allowMult
|
|||
allowMultipleSelections.object(), allowMultiple);
|
||||
}
|
||||
|
||||
QStringList nameFilterExtensions(const QString nameFilters)
|
||||
{
|
||||
QStringList ret;
|
||||
#if QT_CONFIG(regularexpression)
|
||||
QRegularExpression re("(\\*\\.?\\w*)");
|
||||
QRegularExpressionMatchIterator i = re.globalMatch(nameFilters);
|
||||
while (i.hasNext())
|
||||
ret << i.next().captured(1);
|
||||
#endif // QT_CONFIG(regularexpression)
|
||||
ret.removeAll("*");
|
||||
return ret;
|
||||
}
|
||||
|
||||
void QAndroidPlatformFileDialogHelper::setMimeTypes()
|
||||
{
|
||||
m_intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;",
|
||||
QJNIObjectPrivate::fromString("*/*").object());
|
||||
QStringList mimeTypes = options()->mimeTypeFilters();
|
||||
const QString nameFilter = options()->initiallySelectedNameFilter();
|
||||
|
||||
if (mimeTypes.isEmpty() && !nameFilter.isEmpty()) {
|
||||
QMimeDatabase db;
|
||||
for (const QString &filter : nameFilterExtensions(nameFilter))
|
||||
mimeTypes.append(db.mimeTypeForFile(filter).name());
|
||||
}
|
||||
|
||||
QString type = !mimeTypes.isEmpty() ? mimeTypes.at(0) : QLatin1String("*/*");
|
||||
m_intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;",
|
||||
QJNIObjectPrivate::fromString(type).object());
|
||||
|
||||
if (!mimeTypes.isEmpty()) {
|
||||
const QJNIObjectPrivate extraMimeType = QJNIObjectPrivate::getStaticObjectField(
|
||||
JniIntentClass, "EXTRA_MIME_TYPES", "Ljava/lang/String;");
|
||||
|
||||
QJNIObjectPrivate mimeTypesArray = QJNIObjectPrivate::callStaticObjectMethod(
|
||||
"org/qtproject/qt5/android/QtNative",
|
||||
"getStringArray",
|
||||
"(Ljava/lang/String;)[Ljava/lang/String;",
|
||||
QJNIObjectPrivate::fromString(mimeTypes.join(",")).object());
|
||||
|
||||
const QJNIObjectPrivate extraMimeType = QJNIObjectPrivate::getStaticObjectField(
|
||||
JniIntentClass, "EXTRA_MIME_TYPES", "Ljava/lang/String;");
|
||||
for (const QString &type : options()->mimeTypeFilters()) {
|
||||
m_intent.callObjectMethod(
|
||||
"putExtra", "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
|
||||
extraMimeType.object(), QJNIObjectPrivate::fromString(type).object());
|
||||
"putExtra", "(Ljava/lang/String;[Ljava/lang/String;)Landroid/content/Intent;",
|
||||
extraMimeType.object(), mimeTypesArray.object());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue