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 <bogdan@kdab.com>
This commit is contained in:
parent
c20f92a0fa
commit
2f7d3cf559
@ -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();
|
||||
|
@ -33,8 +33,9 @@
|
||||
|
||||
#include "qandroidplatformservices.h"
|
||||
#include <QUrl>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
#include <QMimeDatabase>
|
||||
#include <QtCore/private/qjni_p.h>
|
||||
|
||||
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<jboolean>(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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user