【问题标题】:Custom file extension in AndroidAndroid中的自定义文件扩展名
【发布时间】:2014-12-18 13:36:45
【问题描述】:

我有一个像 .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)似乎根本无法识别自定义文件结尾。

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

【问题讨论】:

    标签: android android-manifest intentfilter file-extension


    【解决方案1】:

    文件的模式很混乱......似乎是开发人员故意做的。 似乎文件模式在不同设备上的工作方式不同,但这仅取决于文件结构的深度。见此链接:pathPattern to match file extension does not work if a period exists elsewhere in the file name?

    【讨论】:

    • 感谢您的建议。但这绝对不是问题,因为我已经尝试过您的建议。
    【解决方案2】:

    我认为这种方式对我有用(使用您要使用的扩展名更改“自定义”):

       <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:
                http://stackoverflow.com/questions/3400072/pathpattern-to-match-file-extension-does-not-work-if-a-period-exists-elsewhere-i/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 我不记得了。对不起
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 2019-08-28
    • 2010-10-28
    相关资源
    最近更新 更多