add a couple more helper functions about files

This commit is contained in:
tibbi 2016-10-16 17:32:48 +02:00
parent 61dee85c0d
commit 6aaca003a9
2 changed files with 51 additions and 0 deletions

View file

@ -44,6 +44,10 @@ class PropertiesDialog : DialogFragment() {
properties_resolution_label.visibility = View.VISIBLE
properties_resolution.visibility = View.VISIBLE
properties_resolution.text = mItem.resolution
} else if (mItem.isAudio(context)) {
} else if (mItem.isVideo(context)) {
}
val file = File(mItem.path)

View file

@ -1,7 +1,12 @@
package com.simplemobiletools.filepicker.models
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.MediaMetadataRetriever
import android.net.Uri
import java.io.File
import java.util.*
class FileDirItem(val path: String, val name: String, val isDirectory: Boolean, val children: Int, val size: Long) :
Comparable<FileDirItem> {
@ -27,6 +32,33 @@ class FileDirItem(val path: String, val name: String, val isDirectory: Boolean,
return options.outWidth != -1 && options.outHeight != -1
}
fun isVideo(context: Context): Boolean {
return getMimeType(context).startsWith("video")
}
fun isAudio(context: Context): Boolean {
return getMimeType(context).startsWith("audio")
}
fun getMimeType(context: Context): String {
try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(context, Uri.fromFile(File(path)))
return retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE)
} catch (ignored: Exception) {
}
return ""
}
fun getDuration(context: Context): String {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(context, Uri.fromFile(File(path)))
val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
val timeInMillisec = java.lang.Long.parseLong(time)
return getFormattedDuration((timeInMillisec / 1000).toInt())
}
val resolution: String
get () {
val bitmap: Bitmap? = BitmapFactory.decodeFile(path)
@ -35,4 +67,19 @@ class FileDirItem(val path: String, val name: String, val isDirectory: Boolean,
return "${bitmap.width} x ${bitmap.height}"
}
private fun getFormattedDuration(duration: Int): String {
val sb = StringBuilder(8)
val hours = duration / (60 * 60)
val minutes = duration % (60 * 60) / 60
val seconds = duration % (60 * 60) % 60
if (duration > 3600) {
sb.append(String.format(Locale.getDefault(), "%02d", hours)).append(":")
}
sb.append(String.format(Locale.getDefault(), "%02d", minutes))
sb.append(":").append(String.format(Locale.getDefault(), "%02d", seconds))
return sb.toString()
}
}