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