2011-04-21 136 views
33

我想要什么:为了能够通过邮件发送我的自定义文件并将其与我的应用程序从GMail中的预览按钮或在文件浏览器中打开时导入。如何添加自定义MIME类型?

我知道:我读过很多自定义MIME类型的处理程序,Android不关心文件扩展等,但如何为我的自定义文件创建MIME类型?

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

某些方向如何注册/映射自定义MIME类型将被处理! :)

+0

意图过滤器应该足够好。 看看http://stackoverflow.com/questions/1733195/android-intent-filter-for-a-particular-file-extension – pinxue 2011-12-20 08:22:13

+0

你不需要创建自己的mimeType。您可以使用一些文件扩展名。看看这个答案https://stackoverflow.com/a/2062112/1298357。这非常有帮助! – 2012-03-29 18:10:52

回答

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> 
+0

你可以看看这个问题吗?这是一些什么相关的问题在这个线程http://stackoverflow.com/questions/16441330/declaring-mime-type-for-a-custom-file-that-is-to-be-sent-via-bluetooth http://stackoverflow.com/questions/16413498/blue-tooth-file-not- – neerajDorle 2013-05-10 04:46:19

+1

-1因为它不回答问题。 Mackan询问如何向系统注册一个自定义MIME类型,并描述如何为已经注册的MIME类型打开一个活动。 – sven 2013-06-08 10:43:54

-2

使用android.webkit.MimeTypeMap

+1

你能提供一个如何做到这一点的例子吗? – 2013-10-16 21:19:36

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 
--> 

现在注册自定义MIME类型,当你接收短信与文本"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");  
} 
+0

你能看看这个问题吗?这是一些什么相关的问题在这个线程http://stackoverflow.com/questions/16441330/declaring-mime-type-for-a-custom-file-that-is-to-be-sent-via-bluetooth http://stackoverflow.com/questions/16413498/blue-tooth-file-not- – neerajDorle 2013-05-10 04:47:16

+0

Downvoted,因为问题是关于使用自定义MIME类型注册文件扩展名,而不是如何创建自定义意图过滤器(这个答案提供了什么)。 – rsp1984 2016-12-14 15:54:31

3

据我所知,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");,但在某些电话上,我看到实际数据抵达时为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时,您知道这并不是您窃取的任何应用程序的特定数据......但是,您应该在之后验证数据,而不是假设它适用于您的应用程序。

相关问题