Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom mime type?

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! :)

like image 838
Mackan Avatar asked Apr 21 '11 13:04

Mackan


People also ask

How do I add a MIME type?

In the Connections pane, go to the site, application, or directory for which you want to add a MIME type. In the Home pane, double-click MIME Types. In the MIME Types pane, click Add... in the Actions pane. In the Add MIME Type dialog box, add the file name extension and MIME type, and then click OK.

Where do I put MIME type in html?

Look for a <meta> element in the page source that gives the MIME type, for example <meta http-equiv="Content-Type" content="text/html"> . According to the standards, the <meta> element that specifies the MIME type should be ignored if there's a Content-Type header available.

What is MIME type in html?

A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) indicates the nature and format of a document, file, or assortment of bytes. MIME types are defined and standardized in IETF's RFC 6838.


1 Answers

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.

like image 158
DigCamara Avatar answered Sep 18 '22 19:09

DigCamara