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()
);
}
}