android - Android注册应用打开自定义扩展文件(也分享)
问题描述
我试图让我的应用程序共享和处理自定义文件扩展名,单击它后,它将打开我的应用程序然后我解析它。
我已经研究过不同的方式,例如LIKE THIS和THIS,但是从 FileBrowser 或 WhatsApp 中单击文件并没有检测到我的应用程序。
如果有帮助,我正在使用导航组件
我不确定我做错了什么,如果有人有一个可行的例子,我将不胜感激。
谢谢。
这是我尝试过的一些代码(我正在使用 txt 进行测试,因为它是一个简单的扩展)(1)
<intent-filter
android:icon="@mipmap/ic_hp_launcher"
android:label="@string/app_name"
android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:host="*" />
<data android:pathPattern="*.txt" />
</intent-filter>
<intent-filter
android:icon="@mipmap/ic_hp_launcher"
android:label="@string/app_name"
android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:host="*" />
<data android:mimeType="application/txt" />
</intent-filter>
我也试过(2)
<intent-filter
android:icon="@mipmap/ic_hp_launcher"
android:label="@string/app_name"
android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:host="*" />
<data android:pathPattern=".*\\.txt" />
</intent-filter>
<intent-filter
android:icon="@mipmap/ic_hp_launcher"
android:label="@string/app_name"
android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="*"
android:mimeType="*/*"
android:pathPattern=".*\\.txt"
android:scheme="file" />
</intent-filter>
<intent-filter
android:icon="@mipmap/ic_hp_launcher"
android:label="@string/app_name"
android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:scheme="content" />
<data android:mimeType="*/*" />
<!--
Work around Android's ugly primitive PatternMatcher
implementation that can't cope with finding a . early in
the path unless it's explicitly matched.
-->
<data android:host="*" />
<data android:pathPattern=".*\\.txt" />
<data android:pathPattern=".*\\..*\\.txt" />
<data android:pathPattern=".*\\..*\\..*\\.txt" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.txt" />
<!-- keep going if you need more -->
</intent-filter>
(3)
<intent-filter
android:icon="@mipmap/ic_hp_launcher"
android:label="@string/app_name"
android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:mimeType="text/plain" />
</intent-filter>
解决方案
我终于解决了我的问题,我同时测试了多个东西。我最终使用
<!-- this is needed for apps like whatsapp, it doesn't provide extension, so you can't add path -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.OPENABLE" />
<data
android:mimeType="application/octet-stream"
android:scheme="content" />
</intent-filter>
<!-- this is needed for apps like explorer -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.OPENABLE" />
<data
android:host="*"
android:mimeType="*/*"
android:pathPattern=".*\\.ext"
android:scheme="content" />
</intent-filter>
如果您使用mimeType="*/*
然后检查 logcat(根本没有过滤器),搜索content://
然后您会看到 Android 发送到您的应用程序的内容,然后修改 IntentFilter 以匹配您想要的内容。某些应用程序不发送扩展名,因此 usingpathPattern
无法使用它,还schema = content
需要using
感谢@CommonsWare 的帮助。
推荐阅读
- c# - How do you make a C# server that can store its connection to clients AND update them when needed
- python - Python 我可以使用 Tkinter 打印到打印机吗
- python - Problem with KerasRegressor & multiple output
- python-3.x - Anaconda 不启动 python 程序
- c++ - 安装没有 lib 文件的 OpenCV
- function - 将 Vue 路由参数转为整数
- python - 如何安装 mat2vec Python 包?
- python - 如何使用 Python 执行 pickle 序列化类远程主机
- python - Pygame:AttributeError:'pygame.Surface'对象没有属性'display'
- html - 修改Html表单提交输出