Android意图排除特定的文件扩展名?

时间:2017-08-14 08:58:22

标签: android android-intent android-manifest

我希望我的活动能够打开没有任何扩展名的文件和包含以下两个扩展名的文件:class AuthAssets extends Controller { def at(path: String, file: String) = IfAuthorized(Editor) { authReq => val contentStream = this.getClass.getResourceAsStream(path + file) Ok.chunked(Enumerator.fromStream(contentStream)) } } .ext1

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

.ext2

如何包含没有任何扩展名的文件并忽略带扩展名的文件?

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

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

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

1 个答案:

答案 0 :(得分:0)

让您的应用打开 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>