Reputation: 23
I'll currently develop an Xamarin app cross platform (iOS/Android) that download a file via CrossDownloadManager and decompress the zip with SharpZipLib.Portable.
All works fine, but I want to check the download file mime type before send it to the unzip library to avoid any problem. I cannot use the extension of file because is not required.
Upvotes: 1
Views: 3188
Reputation: 2401
*Use MimeTypes Nuget Package: https://www.nuget.org/packages/MimeTypes/
You just have to pass file name to get its content-type:
var mimeType = MimeTypes.GetMimeType(fileName);
This is how I get the mime type of image picked using Image picker's FinishedPickingMedia method in Xamarin.iOS [C#]
NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceURL")] as NSUrl;
if (referenceURL != null)
{
var fileName = referenceURL.Path.ToString();
var url = referenceURL.ToString();
Console.WriteLine(referenceURL.ToString());
}
ALAssetsLibrary assetsLibrary = new ALAssetsLibrary();
assetsLibrary.AssetForUrl(referenceURL, delegate (ALAsset asset)
{
ALAssetRepresentation representation = asset.DefaultRepresentation;
if (representation!= null)
{
string fileName = representation.Filename;
var mimeType = MimeTypes.GetMimeType(fileName);
}
}, delegate (NSError error) {
Console.WriteLine("User denied access to photo Library... {0}", error);
});
Upvotes: 0
Reputation: 133
According to Curiousity's answer, this is how you do it:
public String getMimeType(Uri uri) {
String mimeType = null;
if (uri.Scheme.Equals(ContentResolver.SchemeContent))
{
ContentResolver cr = Application.Context.ContentResolver;
mimeType = cr.GetType(uri);
}
else
{
String fileExtension = MimeTypeMap.GetFileExtensionFromUrl(uri.ToString());
mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(
fileExtension.ToLower());
}
return mimeType;
}
Upvotes: 2