This method returns the file extension (jpg, png, pdf, epub, etc.).
public static String getMimeType(Context context, Uri uri) { String extension; //Check uri format to avoid null if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) { //If scheme is a content final MimeTypeMap mime = MimeTypeMap.getSingleton(); extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri)); } else { //If scheme is a File //This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters. extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString()); } return extension; }
Aaron Apr 09 '16 at 9:09 2016-04-09 09:09
source share