Android:将应用与自定义文件类型

时间:2015-12-15 22:15:22

标签: android file-association

我有一个文件类型,扩展名是'.rfts'(实际上它只是存储一个代表音频放大器用户配置的JSON字符串)。我希望能够在电子邮件附件(例如Gmail)中打开此文件,以便我可以从其他平板电脑导入用户设置。

这是我的清单的样子(请注意,我没有在其中包含其他活动,但还有其他4个没有意图过滤器。)

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/RFtheme" >
    <activity
        android:name=".activity.MainActivity"
        android:screenOrientation="landscape"
        android:windowSoftInputMode="adjustPan" >
        <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"/>
            <category android:name="android.intent.category.OPENABLE"/>
            <data android:scheme="file"/>
            <data android:mimeType="*/*"/>

            <data android:pathPattern="\\.rfts$"/>
            <data android:host="*"/>
        </intent-filter>
    </activity>

    <provider
        android:name=".model.FileProvider"
        android:authorities="com.rockfordcorp.app3sixty.provider"
        android:enabled="true"
        android:exported="true" >
    </provider>
</application>

我一直在尝试其他问题的其他几个建议修复,但它们用于从浏览器打开pdf等内容。

当我尝试在Gmail中打开.rfts附件时,它告诉我“你没有可以打开此文件的应用程序(.rfts)。尝试搜索谷歌播放可以”

我不知道自己需要做什么。我不知道Gmail会用什么来打开.rfts,也不知道它会使用什么方案。我尝试了几种不同的组合,但没有真正奏效。我还没有把Android正在寻找的类别,mimetype,模式和方案的神奇组合放到我的应用程序中。

编辑 取得了一些成功,但尚未完成

建议修正的问题不合适,原因是所需的方案实际上是'内容',而不是'文件'

有效的意图过滤器是

<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:mimeType="*/*" android:pathPattern=".*\\.rfts"/>
            <data android:scheme="content" android:pathPattern=".*\\.rfts" android:mimeType="application/octet-stream" />
            <!-- <data android:host="*"/> -->
        </intent-filter>

没有 android:sceheme =“content”它不起作用。

然而,出现了一个新问题。 Gmail现在会打开以前未与其他应用关联的所有文件类型。例如,如果我尝试打开 .rfff 文件,它会使用我的应用程序。如果您尝试打开 .txt ,则会打开Chrome或HTML查看器的选择器。

这很接近,但是打开其他文件类型是有问题的。 Android:pathPattern显然对我的应用程序所关联的文件类型没有影响。

由于此问题被标记为可能重复,我想指出建议的解决方案不适用于从g-mail而不是web打开文件,也不包括打开自定义文件类型。使用交换出的文件类型的“解决方案”会导致g-mail继续坚持设备上没有能够打开文件类型的应用程序。

可能需要提供一个不同的解决方案,以通过Gmail的意图关联打开此自定义文件类型。

1 个答案:

答案 0 :(得分:0)

更新2020

Android已转向用于意图过滤器的内容URI和MIME类型。

问题

内容URI不一定必须包含文件的扩展名或名称,并且在提供内容/文件的不同应用程序中它会有所不同。

以下是来自相同电子邮件附件的不同电子邮件应用程序的一些示例内容URI:

Gmail-> content://com.google.android.gm.sapi/some_email@gmail.com/message_attachment_external/%23thread-a%3Ar332738858767305663/%23msg-a%3Ar-5439466788231005876/0.1?account_type=com.google&mimeType=application%2Foctet-stream&rendition=1

Outlook-> content://com.microsoft.office.outlook.fileprovider/outlookfile/data/data/com.microsoft.office.outlook/cache/file-download/file--2146063402/filename.customextention

三星电子邮件应用程序-> content://com.samsung.android.email.attachmentprovider/1/1/RAW

可以看到它们都是不同的,并且不能保证包含与您的实际文件有关的任何内容。因此,您不能像大多数建议那样使用android:pathPattern

电子邮件附件解决方案

<intent-filter>
    <action android:name="android.intent.action.VIEW"/>

    <category android:name="android.intent.category.BROWSABLE"/>
    <category android:name="android.intent.category.DEFAULT"/>

    <data android:scheme="content"/>
    <data android:host="*"/>

    <!--  Required for Gmail and Samsung Email App  -->
    <data android:mimeType="application/octet-stream"/>

    <!--  Required for Outlook  -->
    <data android:mimeType="application/my-custom-extension"/>
</intent-filter>

通过测试,我发现了Gmail,Outlook和Samsung Email使用的MIME类型,并将它们添加到我的意图过滤器中。

注意事项/陷阱

  • 我发现,使用上述解决方案,如果打开任何二进制类型的文件,它将自动启动我的应用程序。如果无法解析文件,我会在活动中显示失败状态来处理此问题。我认为这是非常罕见的事件,因此可以接受。

  • 在找不到将<data android:mimeType="*/*"/>添加到我的意图过滤器的情况下,我找不到通过文件浏览器启动我的应用程序的任何方法。我无法使用它,因为只要用户单击手机上的任何文件(不仅是自定义文件扩展名的文件),它就会启动我的应用程序。我不建议将其添加到您的意图过滤器中。

  • 我根本没有使用android:scheme="file"的运气。

  • 我在Android 10上的Samsung Galaxy S10上进行了测试

最终思想

  • 目前尚无优雅的解决方案来将您的应用与Android中的特定扩展名类型相关联。这是我所能做的最好的事情。