如何添加自定义mime类型?

时间:2011-04-21 13:45:07

标签: android mime-types

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

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

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

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

5 个答案:

答案 0 :(得分: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");    
}

答案 1 :(得分:3)

据我所知,mime类型非常灵活(我将其创建为application/whatever)并且它们立即被Android接受,远至Dalvik 2.1版。为了正确处理它们,我添加了这个intent-filter:

<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并检查了它值直接Intent currentIntent = getIntent();)。接收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 :(得分: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 :(得分:0)

我在android通讯录中添加了自定义mime类型。经过长期的研究,我决定与大家分享这一点,我已经在包括android 9.0在内的所有Android手机上进行了测试。

here is my Github link

答案 4 :(得分:-2)

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