在Android中注册新的文件类型

在Android中注册新的文件类型,android,file-type,Android,File Type,我想在Android上编写一个简单的STL(几何数据文件)查看器应用程序,但我无法使系统识别一种格式。我在我的应用程序清单文件中写到: <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.EDIT" /> <action android:name="andro

我想在Android上编写一个简单的STL(几何数据文件)查看器应用程序,但我无法使系统识别一种格式。我在我的应用程序清单文件中写到:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.EDIT" />
    <action android:name="android.intent.action.PICK" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" />
    <data android:pathPattern=".*\\.stl" />
    <data android:mimeType="application/sla" />
    <data android:host="*" />
</intent-filter>

但是,当我启动浏览器并去下载一些示例STL文件时,下载被中断,据报告,系统的数据文件类型未知

我没有真正的Android设备,所以我只使用了一个模拟器,在开发中我使用C#on MonoDroid(但我真的认为这不是问题)


如何解决此问题?

我正在使用此清单向应用程序注册(例如)一个.stl文件类型:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.test.core" android:versionCode="1" android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Testy" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="ThorActivity" android:label="@string/app_name">
        </activity>

        <activity android:name="LokiActivity" android:label="@string/app_name">
        </activity>

        <activity android:name="OdinActivity" android:label="@string/app_name">
            <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="http" android:host="*"
                    android:pathPattern=".*\\.stl" />
                <data android:scheme="https" android:host="*"
                    android:pathPattern=".*\\.stl" />
                <data android:scheme="content" android:host="*"
                    android:pathPattern=".*\\.stl" />
                <data android:scheme="file" android:host="*"
                    android:pathPattern=".*\\.stl" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

</manifest>
然后我打开它,从中读出:

FileOutputStream out = new FileOutputStream(new File(filePath));
我很惊讶这会奏效。因为它在一个
意图过滤器中有多个
数据
条目
,对我不起作用。最终,在一个
活动中使用了多个
意图过滤器

<activity
  android:description='@string/Activity_Description'
  android:icon='@drawable/ic_launcher'
  android:label='@string/Activity_Name'
  android:name='net.sourceforge.uiq3.fx603p.Calculator_Activity'
>
  <intent-filter>
    <action
      android:name='android.intent.action.MAIN'
    ></action>
    <category
      android:name='android.intent.category.LAUNCHER'
    ></category>
  </intent-filter>
  <intent-filter
    android:icon='@drawable/ic_fx_603p_pf'
    android:label='FX-603P Simulator Program'
    android:priority='1'
  >
    <category
      android:name='android.intent.category.DEFAULT'
    ></category>
    <action
      android:name='android.intent.action.VIEW'
    ></action>
    <data
      android:host='*'
      android:pathPattern='.*\\.pf'
      android:scheme='file'
    ></data>
  </intent-filter>
  <intent-filter
    android:icon='@drawable/ic_fx_603p_df'
    android:label='FX-603P Simulator Datafile'
    android:priority='1'
  >
    <category
      android:name='android.intent.category.DEFAULT'
    ></category>
    <action
      android:name='android.intent.action.VIEW'
    ></action>
    <data
      android:host='*'
      android:pathPattern='.*\\.df'
      android:scheme='file'
    ></data>
  </intent-filter>
  <intent-filter
    android:icon='@drawable/ic_fx_603p_af'
    android:label='FX-603P Simulator Allfile (Data and Program)'
    android:priority='1'
  >
    <category
      android:name='android.intent.category.DEFAULT'
    ></category>
    <action
      android:name='android.intent.action.VIEW'
    ></action>
    <data
      android:host='*'
      android:pathPattern='.*\\.af'
      android:scheme='file'
    ></data>
  </intent-filter>
  <intent-filter
    android:icon='@drawable/ic_fx_603p_pf'
    android:label='FX-603P Simulator Program'
    android:priority='1'
  >
    <category
      android:name='android.intent.category.DEFAULT'
    ></category>
    <action
      android:name='android.intent.action.VIEW'
    ></action>
    <data
      android:host='*'
      android:mimeType='application/x-fx-602p.program'
    ></data>
  </intent-filter>
  <intent-filter
    android:icon='@drawable/ic_fx_603p_df'
    android:label='FX-603P Simulator Datafile'
    android:priority='1'
  >
    <category
      android:name='android.intent.category.DEFAULT'
    ></category>
    <action
      android:name='android.intent.action.VIEW'
    ></action>
    <data
      android:host='*'
      android:mimeType='application/x-fx-602p.data'
    ></data>
  </intent-filter>
  <intent-filter
    android:icon='@drawable/ic_fx_603p_af'
    android:label='FX-603P Simulator Allfile (Data and Program)'
    android:priority='1'
  >
    <category
      android:name='android.intent.category.DEFAULT'
    ></category>
    <action
      android:name='android.intent.action.VIEW'
    ></action>
    <data
      android:host='*'
      android:mimeType='application/x-fx-602p.all'
    ></data>
  </intent-filter>
</activity>

示例中缺少文件的实际加载。它应该在“打开文件”日志命令之后插入。

我尝试了其他解决方案,这是唯一适合我的解决方案:

<intent-filter
    android:icon="@drawable/icon"
    android:label="Armro File"
    android:priority="1" >
    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data android:scheme="http" />
    <data android:scheme="https" />
    <data android:scheme="ftp" />
    <data android:scheme="file" />
    <data android:host="*" />
    <data android:mimeType="*/*" />
    <data android:pathPattern=".*\\.myowntype" />
</intent-filter>


为什么其他的不起作用?

试着识别以下格式:

<intent-filter android:priority="999">
    <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="*" />
    <data android:mimeType="application/octet-stream" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.yourextension" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\.yourextension" />
    <data android:pathPattern=".*\\..*\\..*\\.yourextension" />
    <data android:pathPattern=".*\\..*\\.yourextension" />
    <data android:pathPattern=".*\\.yourextension" />
    <data android:scheme="content" />
</intent-filter>

我遇到的
意图过滤器有几个陷阱,但我最终在这里找到了一个有效的例子:在Tim Autin的回答中

  • Android的路径模式匹配器是以非贪婪的方式实现的,这就是为什么一些答案建议添加几个路径模式(请参见)
    对我来说,以斜杠开头的模式效果最好(即
  • mime类型筛选器和文件名筛选器的组合恰好筛选了太多的内容,因此我最终使用了一个用于文件扩展名的意图筛选器和另一个用于mime类型的筛选器
  • 对于文件扩展名,我不知何故需要两个intent过滤器:一个用于通配符mimetype
    ,另一个用于标记中没有mime类型的intent过滤器

正如Tim Autin在回答中所建议的那样,使用这三个意图过滤器最终会起作用

我只是猜测,但您可以尝试忽略主机元素。此外,logcat可能会向您显示“进入”模式的意图,以便您更好地了解系统正在寻找什么。嗯。。我试着把.mmmm格式改成众所周知的格式(我用的是PDF)。转到我的GMail帐户,尝试从我的一封邮件中下载DPF。我得到了完全相同的否定结果。所以我有一些疑问:我是否需要实现我的自定义ContentProvider来以西班牙语声明我希望将Android将要下载的数据放在哪里?对我来说,这听起来真的很奇怪,对自定义格式的支持非常棘手…:(嗯……你尝试过.pdf吗?因为这是一种非常常见的文件类型,可能它已经注册到另一个应用程序。我不知道你是否可以用你的程序覆盖该设置。你尝试过你想要的文件类型吗,STL?如果你只想在智能手机上关联,而不在乎它,而不是通过代码,你可以一直这样做这是:菜单->设置->应用程序->管理应用程序,然后选择一个默认值,然后单击“清除默认值”按钮。是啊,我也尝试过STL。因此,尝试PDF的原因是,它是众所周知的格式,几乎每个人都支持它。但奇怪的是,即使使用PDF,我也得到了同样的负面结果sult,如我所写。我使用的模拟器不是真正的设备,因此我想知道在这种情况下使用模拟器是否有问题。检查我的第一个答案,我编辑了更多关于如何操作的详细信息。我希望这会有所帮助。我需要包括哪些权限。mime类型是什么我对您感到惊讶:)。您可以看到gnclmorais解决方案仅用于一个且唯一的文件扩展名(stl),您的解决方案是关于不同的文件扩展名(pf、df、af等)。很明显,您必须使用多个意图过滤器,每一个都要使用一个extension@Stan为什么多个
android:scheme
比多个
android:pathPattern
更好?我看不出有什么明显的原因。模糊的原因——是的,总是有这些。但并不明显。请注意,如果查看,您将看到所有示例每个意图只有一个
部分。什么类型的mime类型是“application/x-fx-602p.all”?这是如何构建的?您是否以某种方式注册它以便系统识别它?它是“x-”类型。用户定义的类型。每个人都可以在他或她喜欢的时候定义“x-”类型。我不知道为什么其他类型不起作用,但我只知道这对我也起作用!谢谢
<intent-filter
    android:icon="@drawable/icon"
    android:label="Armro File"
    android:priority="1" >
    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data android:scheme="http" />
    <data android:scheme="https" />
    <data android:scheme="ftp" />
    <data android:scheme="file" />
    <data android:host="*" />
    <data android:mimeType="*/*" />
    <data android:pathPattern=".*\\.myowntype" />
</intent-filter>
<intent-filter android:priority="999">
    <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="*" />
    <data android:mimeType="application/octet-stream" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.yourextension" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\.yourextension" />
    <data android:pathPattern=".*\\..*\\..*\\.yourextension" />
    <data android:pathPattern=".*\\..*\\.yourextension" />
    <data android:pathPattern=".*\\.yourextension" />
    <data android:scheme="content" />
</intent-filter>