From 2f7d3cf559e0bdf8643e213de3a36796f6a88bd3 Mon Sep 17 00:00:00 2001 From: Martin Koller Date: Tue, 4 Aug 2015 21:29:18 +0200 Subject: Android: auto-detect MIME type for local files to make openUrl work [ChangeLog][QtCore][Android] Fixed the opening of a local file using QDesktopServices::openUrl(). For a local file to be viewed with QDesktopServices::openUrl(), Android needs to be given the MIME type otherwise it does not start an Intent to view the file. Task-number: QTBUG-45585 Change-Id: Ifcfce4bff35011f205cfadbdb2b37a1780dac87d Reviewed-by: BogDan Vatra --- .../src/org/qtproject/qt5/android/QtNative.java | 4 +++- .../platforms/android/qandroidplatformservices.cpp | 23 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/android/jar/src/org/qtproject/qt5/android/QtNative.java b/src/android/jar/src/org/qtproject/qt5/android/QtNative.java index 040eba5e42..11b6ad8cdc 100644 --- a/src/android/jar/src/org/qtproject/qt5/android/QtNative.java +++ b/src/android/jar/src/org/qtproject/qt5/android/QtNative.java @@ -100,13 +100,15 @@ public class QtNative } } - public static boolean openURL(String url) + public static boolean openURL(String url, String mime) { boolean ok = true; try { Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); + if (!mime.isEmpty()) + intent.setDataAndType(uri, mime); activity().startActivity(intent); } catch (Exception e) { e.printStackTrace(); diff --git a/src/plugins/platforms/android/qandroidplatformservices.cpp b/src/plugins/platforms/android/qandroidplatformservices.cpp index 2dba6c78e1..412e3e0466 100644 --- a/src/plugins/platforms/android/qandroidplatformservices.cpp +++ b/src/plugins/platforms/android/qandroidplatformservices.cpp @@ -33,8 +33,9 @@ #include "qandroidplatformservices.h" #include -#include +#include #include +#include #include QT_BEGIN_NAMESPACE @@ -43,13 +44,27 @@ QAndroidPlatformServices::QAndroidPlatformServices() { } -bool QAndroidPlatformServices::openUrl(const QUrl &url) +bool QAndroidPlatformServices::openUrl(const QUrl &theUrl) { + QString mime; + QUrl url(theUrl); + + // if the file is local, we need to pass the MIME type, otherwise Android + // does not start an Intent to view this file + if ((url.scheme().isEmpty() && QFile::exists(url.path())) || url.isLocalFile()) { + // a real URL including the scheme is needed, else the Intent can not be started + url.setScheme(QLatin1String("file")); + + QMimeDatabase mimeDb; + mime = mimeDb.mimeTypeForUrl(url).name(); + } + QJNIObjectPrivate urlString = QJNIObjectPrivate::fromString(url.toString()); + QJNIObjectPrivate mimeString = QJNIObjectPrivate::fromString(mime); return QJNIObjectPrivate::callStaticMethod(QtAndroid::applicationClass(), "openURL", - "(Ljava/lang/String;)Z", - urlString.object()); + "(Ljava/lang/String;Ljava/lang/String;)Z", + urlString.object(), mimeString.object()); } bool QAndroidPlatformServices::openDocument(const QUrl &url) -- cgit v1.2.3