add properties extensions

This commit is contained in:
tibbi 2016-12-26 12:31:04 +01:00
parent 7d2cba099f
commit bc441cf2d1
2 changed files with 57 additions and 0 deletions

View file

@ -1,5 +1,6 @@
package com.simplemobiletools.commons.extensions
import android.graphics.BitmapFactory
import android.media.MediaMetadataRetriever
import java.io.File
@ -35,3 +36,51 @@ fun File.getMimeType(default: String = ""): String {
default
}
}
fun File.getDuration(): String {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
val timeInMs = java.lang.Long.parseLong(time)
return (timeInMs / 1000).toInt().getFormattedDuration()
}
fun File.getArtist(): String? {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
return retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)
}
fun File.getAlbum(): String? {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
return retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)
}
fun File.getVideoResolution(): String {
try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
val width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH).toInt()
val height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT).toInt()
return "$width x $height ${getMPx(width, height)}"
} catch (ignored: Exception) {
}
return ""
}
fun File.getImageResolution(): String {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(path, options)
val width = options.outWidth
val height = options.outHeight
return "$width x $height ${getMPx(width, height)}"
}
fun getMPx(width: Int, height: Int): String {
val px = width * height / 1000000.toFloat()
val rounded = Math.round(px * 10) / 10.toFloat()
return "(${rounded}MP)"
}

View file

@ -1,6 +1,8 @@
package com.simplemobiletools.commons.extensions
import android.text.format.DateFormat
import java.text.DecimalFormat
import java.util.*
fun Long.formatSize(): String {
if (this <= 0)
@ -10,3 +12,9 @@ fun Long.formatSize(): String {
val digitGroups = (Math.log10(toDouble()) / Math.log10(1024.0)).toInt()
return "${DecimalFormat("#,##0.#").format(this / Math.pow(1024.0, digitGroups.toDouble()))} ${units[digitGroups]}"
}
fun Long.formatLastModified(): String {
val cal = Calendar.getInstance(Locale.ENGLISH)
cal.timeInMillis = this
return DateFormat.format("dd.MM.yyyy HH:mm", cal).toString()
}