我已经编写了下面的 Intent 过滤器来使用我的应用程序打开一个文本文件。它似乎有效,但只是有时有效。例如,如果我通过电子邮件发送一个文本文件,如果我选择从邮件打开,则不会显示我的应用程序。如果我先选择保存,然后打开,我的应用程序将被显示。与保管箱类似的经历,如果我尝试从保管箱打开,我的应用程序将不会被列为能够打开但是如果我从保管箱导出到 sd 并使用文件管理器打开它,我的应用程序会被列出并且作品。
<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" />
<data android:scheme="http" android:host="*" android:pathPattern=".*\\.txt" />
<data android:scheme="https" android:host="*" android:pathPattern=".*\\.txt" />
<!-- <data android:scheme="content" android:host="*" android:pathPattern=".*\\.txt" /> -->
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\.txt" />
</intent-filter>
最佳答案
Dropbox 和电子邮件应用程序可能使用内容提供程序并且与 pathPattern
不匹配。通常内容提供者不包含文件扩展名,但会使用 mime 类型来指示正在打开的文件类型。如果您打算打开任何 text/plain
文件,而不仅仅是那些具有 .txt
扩展名的文件,那么您最好离开 pathPattern
完全关闭。
<intent-filter>
<data android:mimeType="text/plain" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
关于Android Intent 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12197298/