- Home /
How to make your Android app open xml or custom files?
Hi,
I would like to associate my game with xml files, so when I try to open one on my Android mobile (through file browser or email attachments) my game would be in the list of available apps to do it.
This looks like a very basic question to me, however I can't find a proper answer. I found several related topics on different forums/blogs but nothing that explains exactly how to do it.
My best shot is to add an intent filter in my AndroidManifest.xml file:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.xml" />
<data android:host="*" />
</intent-filter>
But it doesn't work... so right now I'm lost as I don't know if there's something wrong in my filter, or if it's not at the right place in the manifest, or if I'm simply missing something asside from that.
Thanks for your help, any input would be great!
Here's my complete manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:theme="@android:style/Theme.NoTitleBar" android:versionCode="1" android:versionName="1.0" package="com.SunsetBits.BreakReturns" android:installLocation="preferExternal">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true" />
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
<activity android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:label="@string/app_name" android:launchMode="singleTask" android:name="com.unity3d.player.UnityPlayerNativeActivity" android:screenOrientation="sensorPortrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.xml" />
<data android:host="*" />
</intent-filter>
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<activity android:name="com.google.example.games.pluginsupport.SignInHelperActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<activity android:name="com.google.example.games.pluginsupport.SelectOpponentsHelperActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<activity android:name="com.google.example.games.pluginsupport.InvitationInboxHelperActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<meta-data android:name="com.google.android.gms.games.APP_ID" android:value="\ 592577814120" />
<meta-data android:name="com.google.android.gms.appstate.APP_ID" android:value="\ 592577814120" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" />
<uses-feature android:glEsVersion="0x00020000" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.touchscreen" />
<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
</manifest>
After a couple of days working on this issue I figured out what was the problem. In fact, my initial intent filter was correct, but only to open ".xml" files with no extra "." in their complete path. So email attachments (stored in "com.android.email") and files created through my app (stored in "com.mycompany.myproject") were not recognized. It's a known "issue" and I simply had to put some fail-safe path patterns to make it work. Here is my final filter for my custom file type ".btp" but it should work with any extensions:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:host="*" />
<data android:pathPattern=".*\\.btp" />
<data android:pathPattern=".*\\..*\\.btp" />
<data android:pathPattern=".*\\..*\\..*\\.btp" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.btp" />
</intent-filter>
Note #1: a lot of "solutions" found on the Internet ommit the host parameter, without it the pathPattern are simply ignored and your app will be available to open every kind of files.
Note #2: it's not working with Gmail, I tried many of the answers I could find with no success...
Note #3: it's not working from web browser. Again, I tried everything I could with the BROWSABLE category but couldn't make it work.
@Taussane - Ah, thank you for that!! I spent the whole day copy, pasting and tweaking the various versions of this that I found through google in to my manifest file, but yours was finally the one that actually did something :) Your notes also quickly helped me realise why one mp4 file showed my app as a launch option, where.as.another.one.didnt.show.at.all.mp4. Thanks to you, I can end my day feeling like I actually accomplished something (even if it was really your accomplishment, because I literally just changed btp to mp4 and got results).
You rock! Now I just need to work out how to actually receive the file name in the app :)
Answer by Taussane · Feb 08, 2015 at 04:50 PM
Ok, I found this article that gives plenty of details and examples: http://www.vogella.com/tutorials/AndroidIntent/article.html
My filter was wrong but at the right place. Here's how it look in the end:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/xhtml+xml" />
</intent-filter>
For information: using the mime type "text/xml" was working great for the SEND action, but not for the VIEW one, I don't know why...
Also, I used the appplication "ManifestViewer" with "Simple XML Viewer" to see what was wrong. In their manifest they specify both "application/xhtml+xml" and "text/xml" mime types. However I didn't see any differences with or without "text/xml" so I removed it in my manifest.
I hope this will help others.
My initial goal was to make it work with a custom file extension, I'll continue from there and if I manage to do it I'll add my solution in the comments.
Your answer
Follow this Question
Related Questions
How to handle an intent from another Android App in Unity? 1 Answer
What is the intent type for an Android Unity App? 1 Answer
How can I replace correctly the main activity on the Android Manifest? 0 Answers
I use Unity 5.2.2. Android Manifest It gives error. What can I do ? 0 Answers
Executing C# Code on BOOT_COMPLETED 1 Answer