注册一个Android应用以接收某些文件类型

时间:2019-02-10 20:22:02

标签: android android-intent mime-types intentfilter

我希望我的应用程序能够接收电子名片。通常来自电子邮件附件,但也包含文件等。不幸的是,它没有显示在“打开方式” /“共享方式”菜单中。

这是清单中的活动定义:

     <activity
        android:name=".MyMainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter tools:ignore="AppLinkUrlError">
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.BROWSABLE"/>
            <action android:name="android.intent.action.EDIT" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
            <data android:scheme="http" android:host="*" android:pathPattern=".*\\.vcf"/>
            <data android:scheme="https" android:host="*" android:pathPattern=".*\\.vcf"/>
            <data android:scheme="content" android:host="*" android:pathPattern=".*\\.vcf"/>
            <data android:scheme="file" android:host="*" android:pathPattern=".*\\.vcf"/>
        </intent-filter>
    </activity>

我的测试vCard电子邮件附件的扩展名为.vcf,并且电子邮件将模仿类型标记为text/vcard。我使用了*.*模拟类型进行测试-显然,这不适用于生产代码。

上面的代码来自其他stackoverflow问题,博客文章等。我最初从以下内容开始(两者的模仿类型分别为text/vcard*/*

        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>

1 个答案:

答案 0 :(得分:1)

对于Web URL和大多数设备上的目的,这应该与“打开方式”种类的选项一起使用:

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/vcard" />
    </intent-filter>

如果您还希望支持“共享对象”,则可以尝试:

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/vcard" />
    </intent-filter>

请注意,这两个操作之间的内容获取方式有所不同:

  • 对于ACTION_VIEW,它是您getData()上的Intent
  • 对于ACTION_SEND,请在EXTRA_STREAM附加项中查找Uri,或在EXTRA_TEXT附加项中查找实际vCard文本