在安卓系统中自定义文件扩展名

5

我有一个自定义的文件扩展名,比如 .foo,我想要将我的应用与之关联。我创建了以下 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" android:pathPattern=".*\\.foo"/-->
            <data android:scheme="file" android:mimeType="*/*"  android:pathPattern=".*\\.foo" android:host="*"/>
 </intent-filter>

问题在于这种方法在某些设备上无法正常工作。例如,只有以下设备可以使用三星Galaxy S3(Android版本:4.4.2):

<data android:scheme="file" android:mimeType="*/*"  android:pathPattern=".*\\.foo" android:host="*"/>

然而,使用LG G2(Android版本:4.4.2),只有这一行起作用:
<data android:scheme="file" android:pathPattern=".*\\.foo"/>

更糟糕的是,Nexus 7(Android 版本:5.0)似乎完全不能识别自定义文件扩展名。

是否有人遇到过类似问题并知道解决方案?

2个回答

0

1
谢谢你的建议。但这绝对不是问题,因为我已经尝试过你的建议了。 - Agnes Klein

0
我认为这种方法适合我(将“custom”更改为您想要使用的扩展名):
   <activity android:name=".MainActivity">
        <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=".*\\.custom"/>
            <!-- These additional pathPattern blocks are to allow for paths with
            additional periods in them. See:
            https://dev59.com/HHA75IYBdhLWcg3wP2gg#8599921 -->
            <data android:pathPattern=".*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.custom"/>
            <data android:host="*"/>
        </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:pathPattern=".*\\.custom"/>
            <data android:pathPattern=".*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.custom"/>
            <data android:host="*"/>
        </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:mimeType="application/vnd.ni.custom" android:scheme="file"/>
        </intent-filter>
    </activity>

这个 ".ni.custom" 部分是如何形成 "vnd.ni.custom" 的?那是一个包名吗? - sinek
@sinek 我不记得了。抱歉。 - android developer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接