Giter Site home page Giter Site logo

datl4g / mimemagic-android Goto Github PK

View Code? Open in Web Editor NEW
29.0 2.0 1.0 167 KB

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

License: Apache License 2.0

Kotlin 100.00%
mime mime-type mime-types mime-magic android android-library android-lib android-file

mimemagic-android's Introduction

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.

mimemagic-android's People

Contributors

datl4g avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

vaginessa

mimemagic-android's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.