【问题标题】:How to add custom mime type?如何添加自定义 mime 类型?
【发布时间】:2011-08-10 08:07:02
【问题描述】:

我想要什么:能够通过邮件发送我的自定义文件,并通过 GMail 中的预览按钮或在文件浏览器中打开它时将其与我的应用程序一起导入。

我所知道的:我已经阅读了很多自定义 mime 类型处理程序,android 不关心文件扩展名等,但是如何为我的自定义文件创建 mime 类型?

问题:我需要成为内容提供商吗?我只想导入文件(从备份)不提供任何东西。我见过有人为“application/abc”处理程序说它工作正常,但是如何为我的文件“myFile.abc”和 MIME 类型添加该连接?

将不胜感激有关如何注册/映射自定义 mime 类型的一些指导! :)

【问题讨论】:

标签: android mime-types


【解决方案1】:

据我所知,mime 类型非常灵活(我将我的创建为 application/whatever)并且它们立即被 Android 接受,早在 Dalvik 2.1 版。为了正确处理它们,我添加了这个意图过滤器:

<intent-filter>
  <action android:name="android.intent.action.VIEW"/>
  <category android:name="android.intent.category.DEFAULT"/>
  <data android:mimeType="application/whatever" />
</intent-filter>

但有一个警告。尽管我总是使用intent.setType("application/whatever"); 设置发送Intent 的类型,但在某些手机上,我看到到达时的实际数据为application/octet(为了查看值,我分配了传入Intent 并直接检查了它的值@987654325 @)。接收的 Android 设备不知道如何处理传入的数据并告诉我。所以我加了

<intent-filter>
  <action android:name="android.intent.action.VIEW"/>
  <category android:name="android.intent.category.DEFAULT"/>
  <data android:mimeType="application/octet-stream" />
</intent-filter>

这种方法当然会很麻烦,但至少 Gmail 的问题在于它不一定会按照传入的名称写入文件,这使得我选择定义的任何路径都无用。至少对于传入的octet-stream,您知道这不是您窃取的任何应用程序的特定数据......不过,您应该在之后验证数据,而不仅仅是假设它对您的应用程序有效。

【讨论】:

    【解决方案2】:

    未经测试,但这样的东西应该可以工作。将它与您要打开文件的活动一起放入您的 AndroidManifest.xml 中:

    <activity name=".ActivityHere">
        <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="mimeTypeHere" />
        </intent-filter>
    </activity>
    

    【讨论】:

      【解决方案3】:
      <activity
          android:name="MainActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
          <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:host="{your mime}.com"
                     android:scheme="http" >            
              </data>
          </intent-filter>
      </activity>
      
      <!--
      android:scheme="http" will make android "think" thats this is a link
      -->
      

      现在,当您收到带有文字 "http://{your mime}.com" 的短信或单击带有此文字的网络链接时,您的活动 (MainActivity) 将运行。

      你也可以添加参数:

      text = "http://{your mime}.com/?number=111";
      

      然后在 onCreate() 或 onResume() 方法中添加:

      Intent intentURI = getIntent();
      Uri uri = null;   
      String receivedNum = "";  
      Log.d("TAG", "intent= "+intentURI);   
      if (Intent.ACTION_VIEW.equals(intentURI.getAction())) {  
          if (intentURI!=null){     
              uri = intentURI.getData();   
              Log.d("TAG", "uri= "+uri);   
          }   
          if (uri!=null)   
              receivedNum = uri.getQueryParameter("number");    
      }
      

      【讨论】:

      【解决方案4】:

      我在 android 联系人列表中添加了自定义 mime 类型。经过长时间的研究,我决定与大家分享这个,我已经在所有 Android 手机上测试过,包括 android 9.0。

      here is my Github link

      【讨论】:

        【解决方案5】:

        使用 android.webkit.MimeTypeMap 注册自定义 mime 类型

        【讨论】:

        • 你能提供一个例子来说明如何做到这一点吗?
        猜你喜欢
        • 2012-01-19
        • 2013-08-10
        • 2013-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        相关资源
        最近更新 更多