Two ways to get file GetMimeType on Android

Source: Internet
Author: User

Method 1:

Import java.util.locale;private static String getsuffix (file file) {            if (file = = NULL | |!file.exists () | | | file.isdire Ctory ()) {                return null;            }            String fileName = File.getname ();            if (Filename.equals ("") | | Filename.endswith (".")) {                return null;            }            int index = Filename.lastindexof (".");            if (Index! =-1) {                return filename.substring (index + 1). toLowerCase (locale.us);            } else {                return null;            }    } public    static string getmimetype (file file) {          string suffix = getsuffix (file);            if (suffix = = null) {                return "file/*";            }            String type = Mimetypemap.getsingleton (). getmimetypefromextension (suffix);            if (type! = NULL | |!type.isempty ()) {                return type;            }            return "file/*";}

Method 2:

public static string GetMimeType (String filePath) {    mediametadataretriever MMR = new Mediametadataretriever ();    String MIME = "Text/plain";    if (FilePath! = null) {         try {             mmr.setdatasource (filePath);             MIME = Mmr.extractmetadata (Mediametadataretriever.metadata_key_mimetype);         } catch (IllegalStateException e) {    return mime;         } catch (IllegalArgumentException e) {    return mime;} catch (Ru Ntimeexception e) {    return mime;}    }    return MIME;}




Two ways to get file GetMimeType on Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.