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

Mag*_*les 6 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>
Run Code Online (Sandbox Code Playgroud)

我一直在尝试其他问题的其他一些建议修复,但它们用于从浏览器打开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>
Run Code Online (Sandbox Code Playgroud)

没有android:sceheme ="content"它不起作用.

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

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

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

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

小智 5

2020年更新

Android 已转向使用内容 URI 和 MIME 类型来进行意图过滤器。

问题

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

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

邮箱 ->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

展望->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>
Run Code Online (Sandbox Code Playgroud)

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

注意事项/陷阱

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

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

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

  • 我在运行 Android 10 的三星 Galaxy S10 上进行了测试

最后的想法

  • 目前还没有优雅的解决方案可以将您的应用程序与 Android 中的特定扩展类型相关联。这是我在我的情况下能做的最好的事情。