URLUtilCompat


class URLUtilCompat


Compatibility versions of methods in android.webkit.URLUtil.

See also
URLUtil

Summary

Public functions

java-static String?

Extract filename from a Content-Disposition header value.

java-static String
guessFileName(url: String, contentDisposition: String?, mimeType: String?)

Guesses canonical filename that a download would have, using the URL and contentDisposition.

Public functions

getFilenameFromContentDisposition

Added in 1.11.0
java-static fun getFilenameFromContentDisposition(contentDisposition: String): String?

Extract filename from a Content-Disposition header value.

This method implements the parsing defined in RFC 6266, supporting both the filename and filename* disposition parameters. If the passed header value has the "inline" disposition type, this method will return null to indicate that a download was not intended.

If both filename* and filename is present, the former will be returned, as per the RFC. Invalid encoded values will be ignored.

Parameters
contentDisposition: String

Value of Content-Disposition header.

Returns
String?

The filename suggested by the header or null if no filename could be parsed from the header value.

guessFileName

Added in 1.11.0
java-static fun guessFileName(url: String, contentDisposition: String?, mimeType: String?): String

Guesses canonical filename that a download would have, using the URL and contentDisposition.

This method differs from guessFileName in the following ways:

  • This method uses an updated parsing of contentDisposition, making this available on older Android versions. See getFilenameFromContentDisposition.
  • If the filename guessed from url or contentDisposition already contains an extension, but this extension differs from the one expected from the mimeType, then this method will append the expected extension instead of replacing the one already present. This is done to preserve filenames that contain a "." as part of a filename but where the last part is not meant as an extension.
  • If the filename guessed from contentDisposition contains a "/" character, it will be replaced with "_", unlike guessFileName which will only return the part after the last "/" character.

This method will use getFilenameFromContentDisposition to parse the passed contentDisposition.

  • If not file extension is present in the guessed file name, one will be added based on the mimetype (this will be ".bin" if mimeType is null).
  • If the guessed file name already contains an extension, but this extension doesn't match a provided mimeType, then a new file extension will be added that matches the mimeType.
Parameters
url: String

Url to the content. Must not be null

contentDisposition: String?

Content-Disposition HTTP header or null

mimeType: String?

Mime-type of the content or null

Returns
String

suggested filename