2011-04-21 134 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時,您知道這並不是您竊取的任何應用程序的特定數據......但是,您應該在之後驗證數據,而不是假設它適用於您的應用程序。

相關問題