#159 Not able to run local files via QDesktopServices::openUrl

unstable
open
nobody
None
2012-02-17
2012-02-17
TomasL
No

When trying to run a local file on Android, the MIME type needs to be set as well for the Intent.
Thus the QDesktopServices::openUrl((QUrl("file:///sdcard/temp/SCM.apk"))) gives the following error:

I/ActivityManager( 2704): Starting: Intent { act=android.intent.action.VIEW dat=file:///sdcard/temp/SCM.apk } from pid 8656
E/AndroidRuntime( 8656): FATAL EXCEPTION: Thread-10
E/AndroidRuntime( 8656): java.lang.NoClassDefFoundError: org.kde.necessitas.industrius.QtNative$4
E/AndroidRuntime( 8656): at org.kde.necessitas.industrius.QtNative.setFullScreen(QtNative.java:400)
E/AndroidRuntime( 8656): at dalvik.system.NativeStart.run(Native Method)
E/AndroidRuntime( 8656): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///sdcard/temp/SCM.apk}
E/AndroidRuntime( 8656): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
E/AndroidRuntime( 8656): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
E/AndroidRuntime( 8656): at android.app.Activity.startActivityForResult(Activity.java:2833)
E/AndroidRuntime( 8656): at android.app.Activity.startActivity(Activity.java:2959)
E/AndroidRuntime( 8656): at org.kde.necessitas.industrius.QtNative.openURL(QtNative.java:76)
E/AndroidRuntime( 8656): ... 1 more

I think this may be fixed by changing the openURL function in src/android/jar/src/org/kde/necessitas/industrius/QtNative.java from

public static void openURL(String url)
{
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
activity().startActivity(intent);
}

to the following:

public static void openURL(String url)
{
if (url.toLowerCase().startsWith("file:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
MimeTypeMap mime = MimeTypeMap.getSingleton();
File file = new File(url);
String ext=file.getName().substring(file.getName().lastIndexOf(".")+1).toLowerCase();
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file),type);
activity().startActivity(intent);
}
else
{
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
activity().startActivity(intent);
}
}
Must also add the following import:
import android.webkit.MimeTypeMap;

Otherwise one will have to find a mechanism to provide the MIME filetype some other way.

Cheers, TomasL

Discussion