android - 注册 Android 应用程序以接收某些文件类型

标签 android android-intent mime-types intentfilter

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

这是 list 中的 Activity 定义:

     <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 文件扩展名,并且电子邮件将 mimetype 标记为 text/vcard。我使用了 *.* mimetype 进行测试 - 显然这不适合生产代码。

上面的代码是从其他 stackoverflow 问题、博客文章等中抄袭来的。我最初是从以下代码开始的(都是 text/vcard 的 mimetype,然后是 */*:

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

最佳答案

对于 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>

如果你也想支持“Share With”,你可以试试:

    <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,它是 Intent 上的 getData()
  • 对于 ACTION_SEND,在 EXTRA_STREAM extra 中查找 Uri 或在 EXTRA_TEXT extra 中查找实际电子名片文本

关于android - 注册 Android 应用程序以接收某些文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54620647/

相关文章:

android - Android Action Bar Icon Pack 中的大图标在哪里?

android - 如何在组织内部部署 Android 应用程序?

python - 由于 : file does not start with RIFF id,无法将文件 file.wav 作为 WAV 打开

android - 如何在默认浏览器中打开本地html文件

java - Android - 从没有扩展名的文件中获取 MIME 类型

android - 从 Camera Intent 返回会导致 Activity 崩溃

android:引用自定义xml中的资源

java - Android:如何设置 MenuButton 的监听器?

android - URL 值未传递给另一个 Activity

Android:在 Activity 之间传递 HashMap