gpt4 book ai didi

Android Intent 排除特定文件扩展名?

转载 作者:行者123 更新时间:2023-11-29 02:38:31 24 4
gpt4 key购买 nike

我希望我的 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 文件相关联。有什么建议吗?

最佳答案

让您的应用程序打开带有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>

关于Android Intent 排除特定文件扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com