1

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.

BombezMan
  • 23
  • 6
  • What are you going to do if the mime type is incorrect? Just pass the file to the unzip library and let it make sure it's correct. – Neil Sep 25 '18 at 07:57

2 Answers2

1

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;
}
AndroidDev
  • 123
  • 9
0

*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);
});
Divyesh_08
  • 787
  • 6
  • 16