【问题标题】:Android intent exclude a particular file extension?Android意图排除特定文件扩展名?
【发布时间】:2017-08-14 08:58:22
【问题描述】:

我希望我的 Activity 能够打开没有任何扩展名的文件和具有以下两个扩展名的文件:.ext1.ext2

示例:
打开:my_file.ext1、my_file.ext2、some_other_file、another_file
忽略:my_file.pdf、myfile.mp3 等

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="file" />
    <data android:mimeType="*/*"/>
    <data android:host="*" />
    <data android:pathPattern=".*\\.ext1" />
    <data android:pathPattern=".*\\.ext2" />
</intent-filter>

如何包含不带扩展名的文件,忽略带扩展名的文件?

到目前为止,我发现包含没有任何扩展名的文件的唯一解决方案是添加:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.EDIT" />
    <category android:name="android.intent.category.DEFAULT" />
    <data
        android:mimeType="text/plain"
        android:scheme="file"
        android:host="*" />
</intent-filter>

但这也将我的 Activity 与 .txt 文件相关联。有什么建议吗?

【问题讨论】:

  • 您找到解决方案了吗?我也想知道。
  • 不,我认为没有,因为您只能告诉过滤器您想要包含的内容。我刚刚在我的问题中使用了解决方案。副作用是当用户尝试打开文本文件时,我的应用程序也会被列出。我只是检查文件类型(文件的内容)是否是我感兴趣的类型。
  • 它也可以打开 Gmail 附件?
  • 我不知道。我什至没有想到这一点。尝试让我们知道。
  • 对于 Gmail,您需要 android:host="gmail-ls"

标签: android android-intent android-manifest


【解决方案1】:

让您的应用打开带有 NO 扩展名的文件的唯一方法是使用android:mimeType="*/*"。不幸的是,这会使您的应用也打开设备上的所有文件。

对于使用您的应用打开 SPECIFIC 文件扩展名的情况,您需要以下内容:

        <!-- Handle opening attachments with file explorer -->
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:mimeType="application/json"
                  android:pathPattern=".*\\.ext1"
                  android:scheme="file"/>
        </intent-filter>
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*"
                  android:mimeType="*/*"
                  android:pathPattern=".*\\.ext1"
                  android:scheme="content"/>
        </intent-filter>
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:mimeType="application/json"
                  android:pathPattern=".*\\.ext2"
                  android:scheme="file"/>
        </intent-filter>
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*"
                android:mimeType="*/*"
                android:pathPattern=".*\\.ext2"
                android:scheme="content"/>
        </intent-filter>

        <!-- Handle opening attachments with Downloads app -->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.GET_CONTENT"/>
            <action android:name="android.intent.action.OPEN_DOCUMENT"/>
            <action android:name="android.intent.action.PICK"/>
            <category android:name="android.intent.category.OPENABLE"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:mimeType="text/plain" android:pathPattern=".*\.ext1"
                  android:scheme="content"/>
            <data android:mimeType="application/json" android:pathPattern=".*\.ext1"
                  android:scheme="content"/>
            <data android:mimeType="application/octet-stream" android:pathPattern=".*\.ext1"
                  android:scheme="content"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.GET_CONTENT"/>
            <action android:name="android.intent.action.OPEN_DOCUMENT"/>
            <action android:name="android.intent.action.PICK"/>
            <category android:name="android.intent.category.OPENABLE"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:mimeType="text/plain" android:pathPattern=".*\.ext2"
                  android:scheme="file"/>
            <data android:mimeType="application/json" android:pathPattern=".*\.ext2"
                  android:scheme="file"/>
            <data android:mimeType="application/octet-stream" android:pathPattern=".*\.ext2"
                  android:scheme="file"/>
        </intent-filter>

【讨论】:

    猜你喜欢
    • 2010-12-16
    • 2018-03-04
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 2020-11-20
    相关资源
    最近更新 更多