How to add custom mime type?
Asked Answered
K

5

42

What I want: To be able to send my custom file by mail and import it with my application from the preview button in GMail or when opening it in a file browser.

What I know: I've read a lot of custom mime type handlers, that android doesn't care about file extension etc., but how to create the mime type for my custom file?

The question: Do I need to be a content provider? I just want to import files (from backup) not provide anything. I've seen people having handlers for "application/abc" saying it's working fine, but how to add that connection for my file "myFile.abc" and the mime type?

Some direction how to register/map custom mime types would be appreciated! :)

Korn answered 21/4, 2011 at 13:45 Comment(4)
Intent-Filter should be good enough. Have a look at #1733695Attract
You don't realy need to create your own mimeType. You can use some file extension. Look in this answer https://mcmap.net/q/195298/-android-intent-filter-for-a-particular-file-extension . It's very helpful!Norikonorina
any update on this?Agranulocytosis
@EJChathuranga i asked a follow up question #68044381 because android-7 and later does not allow file-filters anymore. No answer yet :-(Aforetime
W
5

As far as I can tell, mime types are pretty flexible (I created mine as application/whatever) and they're accepted immediately by Android, as far back as Dalvik version 2.1. To handle them properly, I added this 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>

There is a caveat though. Even though I always set the type of the send Intent with intent.setType("application/whatever");, on some phones I've seen the actual data on arrival as application/octet (to see the value, I assigned the incoming Intent and inspected its value directly Intent currentIntent = getIntent();). The receiving Android device didn't know what to do with the incoming data and told me so. So I added

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

This approach could be troublesome of course, but the problem with Gmail at least is that it doesn't necessarily write the file with the name as it comes in, which renders any Path I choose to define useless. And at least with an incoming octet-stream you know it's not any app's specific data you're stealing away... Still, you should validate the data afterwards and not just assume it's valid for your app.

Wryneck answered 1/10, 2013 at 5:19 Comment(0)
S
3

I have added custom mime type in android contacts list. After a long research i decided to share this with you guys, i have tested this on all Android cell phone including android 9.0.

here is my Github link

Swisher answered 4/2, 2020 at 7:57 Comment(0)
T
2

Untested, but something like this should work. Put it in your AndroidManifest.xml with the activity you want to open the file:

<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>
Talkfest answered 22/12, 2011 at 22:9 Comment(0)
L
2
<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
-->

Now, when you receiving a sms with the text "http://{your mime}.com" or clicking link on the web with this text, your activity (MainActivity) will run.

You also can add parameters:

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

Then in onCreate() or onResume() methods you'll add:

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");    
}
Likelihood answered 19/4, 2012 at 12:33 Comment(2)
can you please have a look at this question? It is some what related to the question in this thread #16441830 https://mcmap.net/q/197098/-blue-tooth-file-not-sent-errorEskew
Downvoted because the question asks about registering a file extension with a custom MIME-type, not how to create a custom intent-filter (what this answer provides).Getupandgo
A
-4

Register a custom mime type using android.webkit.MimeTypeMap

Arad answered 19/1, 2012 at 5:31 Comment(1)
Can you provide an example of how to do that?Hailstone

© 2022 - 2024 — McMap. All rights reserved.