I want to write a simple STL (geometrical data file) viewer application on Android, but I'm not able to make recognize a format to the system. I wrote this in my app manifest file:
<intent-filter>
<action android开发者_如何学运维:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:pathPattern=".*\\.stl" />
<data android:mimeType="application/sla" />
<data android:host="*" />
</intent-filter>
But the moment I launch the browser and go to download some sample STL file, the download is interrupted and I'm reported that the data file type is unknown for the system.
I have no real Android device, so I use only an emulator, and for development I use C# on MonoDroid (but I honestly don't think that is the problem).
How could I fix this problem?
I'm using this manifest to register (for example) a .stl file type with my application:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.test.core" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Testy" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ThorActivity" android:label="@string/app_name">
</activity>
<activity android:name="LokiActivity" android:label="@string/app_name">
</activity>
<activity android:name="OdinActivity" android:label="@string/app_name">
<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="http" android:host="*"
android:pathPattern=".*\\.stl" />
<data android:scheme="https" android:host="*"
android:pathPattern=".*\\.stl" />
<data android:scheme="content" android:host="*"
android:pathPattern=".*\\.stl" />
<data android:scheme="file" android:host="*"
android:pathPattern=".*\\.stl" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
As you can see, I'm linking the .stl file extension to the activity OdinActivity
. Inside the OdinActivity
, I use the following line to get the file path so I can open it:
filePath = getIntent().getData().getEncodedPath();
Then I just open it to read from it:
FileOutputStream out = new FileOutputStream(new File(filePath));
I am surprised that gnclmorais' solution should work. Because it, having multiple data
entries in one intent-filter
, did not work for me. What did work in the end was multiple intent-filter
in one activity
:
<activity
android:description='@string/Activity_Description'
android:icon='@drawable/ic_launcher'
android:label='@string/Activity_Name'
android:name='net.sourceforge.uiq3.fx603p.Calculator_Activity'
>
<intent-filter>
<action
android:name='android.intent.action.MAIN'
></action>
<category
android:name='android.intent.category.LAUNCHER'
></category>
</intent-filter>
<intent-filter
android:icon='@drawable/ic_fx_603p_pf'
android:label='FX-603P Simulator Program'
android:priority='1'
>
<category
android:name='android.intent.category.DEFAULT'
></category>
<action
android:name='android.intent.action.VIEW'
></action>
<data
android:host='*'
android:pathPattern='.*\\.pf'
android:scheme='file'
></data>
</intent-filter>
<intent-filter
android:icon='@drawable/ic_fx_603p_df'
android:label='FX-603P Simulator Datafile'
android:priority='1'
>
<category
android:name='android.intent.category.DEFAULT'
></category>
<action
android:name='android.intent.action.VIEW'
></action>
<data
android:host='*'
android:pathPattern='.*\\.df'
android:scheme='file'
></data>
</intent-filter>
<intent-filter
android:icon='@drawable/ic_fx_603p_af'
android:label='FX-603P Simulator Allfile (Data and Program)'
android:priority='1'
>
<category
android:name='android.intent.category.DEFAULT'
></category>
<action
android:name='android.intent.action.VIEW'
></action>
<data
android:host='*'
android:pathPattern='.*\\.af'
android:scheme='file'
></data>
</intent-filter>
<intent-filter
android:icon='@drawable/ic_fx_603p_pf'
android:label='FX-603P Simulator Program'
android:priority='1'
>
<category
android:name='android.intent.category.DEFAULT'
></category>
<action
android:name='android.intent.action.VIEW'
></action>
<data
android:host='*'
android:mimeType='application/x-fx-602p.program'
></data>
</intent-filter>
<intent-filter
android:icon='@drawable/ic_fx_603p_df'
android:label='FX-603P Simulator Datafile'
android:priority='1'
>
<category
android:name='android.intent.category.DEFAULT'
></category>
<action
android:name='android.intent.action.VIEW'
></action>
<data
android:host='*'
android:mimeType='application/x-fx-602p.data'
></data>
</intent-filter>
<intent-filter
android:icon='@drawable/ic_fx_603p_af'
android:label='FX-603P Simulator Allfile (Data and Program)'
android:priority='1'
>
<category
android:name='android.intent.category.DEFAULT'
></category>
<action
android:name='android.intent.action.VIEW'
></action>
<data
android:host='*'
android:mimeType='application/x-fx-602p.all'
></data>
</intent-filter>
</activity>
Note that having both pathPattern
and mimeType
in one data
entry did not work either. Last, but not least, I would suggest a few null checks when getting the file name:
/**
* <p>Open calculator and load file (if one was passed).</p>
* @see android.app.Activity#onStart()
*/
@Override
public void onStart ()
{
android.util.Log.d (Calculator_Activity.TAG, "+ onStart");
super.onStart ();
final android.content.Intent intent = getIntent ();
if (intent != null)
{
android.util.Log.d (Calculator_Activity.TAG, "> Got intent : " + intent);
final android.net.Uri data = intent.getData ();
if (data != null)
{
android.util.Log.d (Calculator_Activity.TAG, "> Got data : " + data);
final String filePath = data.getEncodedPath ();
android.util.Log.d (Calculator_Activity.TAG, "> Open file : " + filePath);
// File loading comes here.
} // if
} // if
android.util.Log.d (Calculator_Activity.TAG, "- onStart");
return;
} // onStart
The actual loading of the file is missing from the sample. It should be inserted after the “Open file” log command.
I tried the other solutions and this is the only one which works for me:
<intent-filter
android:icon="@drawable/icon"
android:label="Armro File"
android:priority="1" >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="ftp" />
<data android:scheme="file" />
<data android:host="*" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.myowntype" />
</intent-filter>
Why do the other ones not work?
Try to recognise the format as follows:
<intent-filter android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.OPENABLE" />
<data android:host="*" />
<data android:mimeType="application/octet-stream" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\.yourextension" />
<data android:pathPattern=".*\\.yourextension" />
<data android:scheme="content" />
</intent-filter>
There are several pitfalls with intent-filters
I ran into, but I finally found an example that worked here: Android intent filter not working in Tim Autin's answer
- The path pattern matcher of Android is implemented in a non-greedy way which is why several answers propose to add several path patterns (see pathPattern to match file extension does not work if a period exists elsewhere in the file name?)
For me the patterns starting with a slash worked best (i.e.)
<data android:pathPatern="/.*..*..*..*..*\\.myextension" />
- A combination of mime type filters and file name filters happens to filter too much so I ended up with an intent filter for file extensions and another one for mime types
- For the file extensions I somehow required two intent filters: one for the wildcard mimetype
<data android:mimeType="*/*" />
and one intent filter without mime type in the tag at all.
So like proposed in Tim Autin's answer using these three intent filters worked in the end
精彩评论