improve album fetching too

This commit is contained in:
tibbi 2020-05-06 11:44:29 +02:00
parent db334fcc4a
commit 8760213138
4 changed files with 31 additions and 24 deletions

View file

@ -114,13 +114,13 @@ class PropertiesDialog() {
fileDirItem.getDuration(activity)?.let { addProperty(R.string.duration, it) }
fileDirItem.getTitle(activity)?.let { addProperty(R.string.song_title, it) }
fileDirItem.getArtist(activity)?.let { addProperty(R.string.artist, it) }
fileDirItem.getAlbum()?.let { addProperty(R.string.album, it) }
fileDirItem.getAlbum(activity)?.let { addProperty(R.string.album, it) }
}
fileDirItem.path.isVideoSlow() -> {
fileDirItem.getDuration(activity)?.let { addProperty(R.string.duration, it) }
fileDirItem.getResolution(activity)?.let { addProperty(R.string.resolution, it.formatAsResolution()) }
fileDirItem.getArtist(activity)?.let { addProperty(R.string.artist, it) }
fileDirItem.getAlbum()?.let { addProperty(R.string.album, it) }
fileDirItem.getAlbum(activity)?.let { addProperty(R.string.album, it) }
}
}

View file

@ -787,6 +787,34 @@ fun Context.getArtist(path: String): String? {
}
}
fun Context.getAlbum(path: String): String? {
val projection = arrayOf(
Audio.Media.ALBUM
)
val uri = getFileUri(path)
val selection = if (path.startsWith("content://")) "${BaseColumns._ID} = ?" else "${MediaColumns.DATA} = ?"
val selectionArgs = if (path.startsWith("content://")) arrayOf(path.substringAfterLast("/")) else arrayOf(path)
try {
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
return cursor.getStringValue(Audio.Media.ALBUM)
}
}
} catch (ignored: Exception) {
}
return try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)
} catch (ignored: Exception) {
null
}
}
fun Context.getStringsPackageName() = getString(R.string.package_name)
fun Context.getFontSizeText() = getString(when (baseConfig.fontSize) {

View file

@ -4,7 +4,6 @@ import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Point
import android.media.MediaMetadataRetriever
import android.os.StatFs
import android.provider.MediaStore
import android.text.Spannable
@ -89,26 +88,6 @@ fun String.containsNoMedia() = File(this).containsNoMedia()
fun String.doesThisOrParentHaveNoMedia() = File(this).doesThisOrParentHaveNoMedia()
fun String.getFileArtist(): String? {
return try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(this)
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)
} catch (ignored: Exception) {
null
}
}
fun String.getFileAlbum(): String? {
return try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(this)
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)
} catch (ignored: Exception) {
null
}
}
fun String.getImageResolution(): Point? {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true

View file

@ -119,7 +119,7 @@ open class FileDirItem(val path: String, val name: String = "", var isDirectory:
fun getArtist(context: Context) = context.getArtist(path)
fun getAlbum() = path.getFileAlbum()
fun getAlbum(context: Context) = context.getAlbum(path)
fun getTitle(context: Context) = context.getTitle(path)