2

When sharing an unknown file type with an ACTION_SEND Intent, should */* or application/octet-stream be used when setting the content type?

According to Mozilla's Complete list of MIME types

Two primary MIME types are important for the role of default types:

  • text/plain is the default value for textual files. A textual file should be human-readable and must not contain binary data.
  • application/octet-stream is the default value for all other cases. An unknown file type should use this type. Browsers pay a particular care when manipulating these files, attempting to safeguard the user to prevent dangerous behaviors.

Example

Intent intent = new Intent(Intent.ActionSend);

Uri uri = Uri.FromFile(file);
intent.PutExtra(Intent.ExtraStream, uri);

string fileType = GetMimeTypeByUri(uri);
if (fileType == null)
{
    fileType = "*/*";                      // ?
    fileType = "application/octet-stream"; // ?
    fileType = "application/x-binary"      // ?
}
intent.SetType(fileType);

StartActivity(Intent.CreateChooser(intent, "Send to..."));

where

private String GetMimeTypeByUri(Uri uri)
{
    if (uri.Scheme.Equals(ContentResolver.SchemeContent))
        return ContentResolver.GetType(uri);
    else
        return Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(
            Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(uri.Path).ToLower()
        );
    }
}
Community
  • 1
  • 1
samus
  • 6,102
  • 6
  • 31
  • 69
  • it seems yes by referring this article http://androidsbs.blogspot.com.tr/2014/01/intent-settypestring-type-how-to-set.html – ugur Aug 21 '17 at 21:01
  • You want open an unknown file using a suitable apps? – York Shen Aug 22 '17 at 05:24
  • @YorkShen-MSFT ACTION_GET_CONTENT is for "opening". – samus Aug 22 '17 at 12:48
  • You could read the [official documents](https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND) : Use `*/*` if the MIME type is unknown. – York Shen Aug 23 '17 at 07:09
  • @YorkShen-MSFT ..."this will only allow senders that can handle generic data streams"... I'm not sure exactly what that is, but I could always read the source. – samus Aug 23 '17 at 12:17

1 Answers1

2

fileType = "* / *";

If you use a mime type of "* / *" when you can't determine it from the system(it is null) it fires the appropriate select application dialog.

fileType = "application/octet-stream";

Typically, it will be an application or a document that must be opened in an application, such as a spreadsheet or word processor. The "octet-stream" subtype is used to indicate that a body contains arbitrary binary data

fileType = "application/x-binary"

it's just a non-standard way of restating "octet-stream".

Check the links for details MIME types , MIME Utils

Hope this may hep you.

Janmejoy
  • 2,721
  • 1
  • 20
  • 38