Android 自定义文件扩展名 Intent 不适用于标准文件浏览器

标签 android mime-types intentfilter file-extension file-browser

我的应用指定能够在 list 文件中打开我自己的文件扩展名,如下所示:

<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:host="*"/>
<data android:mimeType="*/*"/>
<data android:pathPattern=".*\\.ext" />
<data android:pathPattern=".*\\..*\\.ext" />
<data android:pathPattern=".*\\..*\\..*\\.ext" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.ext" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.ext" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.ext" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.ext" />

这曾经在标准的 Android 文件浏览器(“我的文件”)上运行良好,并且仍然可以在运行 Android 4.3 的旧测试设备上运行,但在我运行 Android 6 和 8 的其他设备上不起作用。它也适用于带有其他文件浏览器的新设备(尽管不是全部)。

我也尝试过使用相应的 mime 类型的 png 和 pdf,但它不会在能够打开这些文件的应用程序列表中显示我的应用程序。

标准 Android 文件浏览器需要什么才能识别我的应用,以便能够打开带有我的扩展名的文件?

编辑:我也试过

<data android:scheme="content"/>

最佳答案

试试这个(将“自定义”更改为您要使用的扩展名):

   <activity android:name=".MainActivity">
        <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="file"/>
            <data android:mimeType="*/*"/>
            <data android:pathPattern=".*\\.custom"/>
            <!-- These additional pathPattern blocks are to allow for paths with
            additional periods in them. See:
            http://stackoverflow.com/questions/3400072/pathpattern-to-match-file-extension-does-not-work-if-a-period-exists-elsewhere-i/8599921 -->
            <data android:pathPattern=".*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.custom"/>
            <data android:host="*"/>
        </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="file"/>
            <data android:pathPattern=".*\\.custom"/>
            <data android:pathPattern=".*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\..*\\.custom"/>
            <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.custom"/>
            <data android:host="*"/>
        </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:mimeType="application/vnd.ni.custom" android:scheme="file"/>
        </intent-filter>
    </activity>

关于Android 自定义文件扩展名 Intent 不适用于标准文件浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53110192/

相关文章:

android - 禁用即时运行时,Gradle 构建失败并显示 "Could not read path"

c# - xamarin 广播接收器访问 View 模型

android - OpenGL 阴影贴图在对象上的阴影不正确

ruby-on-rails - Bundler 找不到 gem "mime-types"的兼容版本

Android 和用于电子邮件的 BroadcastReceiver

android - 如何通过扫描 NFC 标签启动没有启动器图标的应用程序?

java - Android:发现蓝牙设备时出错

css - 网络服务器 + Chrome v40 + font awesome 不起作用

java - 如何确定正确的 MIME 类型

android - 无法启动服务 Intent