/MimeMagic-Android

Android MimeMagic Library. Getting MimeType of File, ByteArray, InputStream, Uri and Name.

Primary LanguageKotlinApache License 2.0Apache-2.0

MimeMagic-Android

Stars

Forks

License

MimeMagic-Android is an open source MimeType detector library for Android

  • fetch MimeType by File, ByteArray, InputStream, Uri and Name
  • uses fallback methods if fetching fails
  • get prefix and suffix easily
  • check if MimeType is Archive, Audio, Document, Font, Image, Text, Video

Setup

Add this line in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
        ...
    }
}

Add this line in your app build.gradle:

plugins {
    ...
    id 'kotlin-parcelize'
    ...
}

...

dependencies {
    ...
    implementation("com.github.DatL4g:MimeMagic-Android:1.0.3")
    ...
}

Usage

Create MimeData Object using this static functions:

  • fun fromFile(file: File)
  • fun fromFile(file: File, context: Context)
  • fun fromFile(file: File, contentResolver: ContentResolver)
  • fun fromByteArray(byteArray: ByteArray)
  • fun fromByteArray(byteArray: ByteArray, offset: Int)
  • fun fromByteArray(byteArray: ByteArray, offset: Int, length: Int)
  • fun fromInputStream(inputStream: InputStream)
  • fun fromUri(uri: Uri, context: Context)
  • fun fromUri(uri: Uri, context: Context, extension: String)
  • fun fromUri(uri: Uri, contentResolver: ContentResolver)
  • fun fromUri(uri: Uri, contentResolver: ContentResolver, extension: String)
  • fun fromExtension(extension: String)
  • fun fromName(name: String)

Precision with fallback

You get the most exact MimeType when reading bytes. So you should best use the functions: fromFile, fromByteArray and fromInputStream. Reading the bytes does not always work and the MimeType is null then, to prevent this you can use a fallback method. fromFile uses a fallback method under the hood, there are different types of method used depending on which parameters were passed. To get the most precise MimeType with a good fallback method, I recommend fromFile(file: File, context: Context) or fromFile(file: File, contentResolver: ContentResolver). Less precise is fromFile(file: File) because it uses the MimeType that matches the extension as fallback.

Recommended method

val file = File("path/to/file.extension")
val mimeData = MimeData.fromFile(file, context)
// or
val mimeData = MimeData.fromFile(file, context.contentResolver)

Using extension functions

Available for:

  • ByteArray
  • ContentResolver
  • Context
  • File
  • InputStream
  • Uri

Example:

val file = File("path/to/file.extension")
val mimeData = file.getMimeData(context)
// or
val mimeData = file.getMimeData(context.contentResolver)

InputStream

As stated before this method may be more precise and is more likely to fail. Another hint is that the InputStream will be closed afterwards. You won't experience anything when using fromFile or fromByteArray but make sure you don't use the InputStream after calling fromInputStream.

Handling MimeData Information

Available info:

  • mimeType: String?
  • delimiter: Char = '/'
  • prefix: String?
  • suffix: String?
  • isApplication: Boolean
  • isArchive: Boolean
  • isAudio: Boolean
  • isDocument: Boolean
  • isFont: Boolean
  • isImage: Boolean
  • isText: Boolean
  • isVideo: Boolean

Full Example

val file = File("path/to/file.extension")
val mimeData = MimeData.fromFile(file, context)

Log.e("TAG-MimeType", mimeData.mimeType)
Log.e("TAG-Mime is Image", mimeData.isImage.toString())

Maintainers

This project is mantained by:

Avatar Contributor
DatLag

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -m 'Add some feature')
  4. Push your branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

View full license here. In short:

A permissive license whose main conditions require preservation of copyright and license notices. Contributors provide an express grant of patent rights. Licensed works, modifications, and larger works may be distributed under different terms and without source code.