Add album art loading
Add album art loading by decoding the album art URI into bitmaps for each album. I may use glide in the future if just using a bitmap isnt enough, but currently this works.
This commit is contained in:
@@ -10,6 +10,7 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||
import org.oxycblt.auxio.music.MusicRepository
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
|
||||
@@ -28,6 +29,10 @@ class LibraryFragment : Fragment() {
|
||||
|
||||
libraryModel
|
||||
|
||||
val albums = MusicRepository.getInstance().albums.value
|
||||
|
||||
binding.testAlbum.setImageBitmap(albums?.get((0..albums.size).random())?.cover)
|
||||
|
||||
Log.d(this::class.simpleName, "Fragment created.")
|
||||
|
||||
return binding.root
|
||||
|
||||
@@ -6,12 +6,12 @@ import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.oxycblt.auxio.music.processing.MusicLoader
|
||||
import org.oxycblt.auxio.music.processing.MusicLoaderResponse
|
||||
import org.oxycblt.auxio.music.processing.MusicSorter
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
import org.oxycblt.auxio.music.processing.MusicLoader
|
||||
import org.oxycblt.auxio.music.processing.MusicLoaderResponse
|
||||
import org.oxycblt.auxio.music.processing.MusicSorter
|
||||
|
||||
// Storage for music data.
|
||||
class MusicRepository {
|
||||
|
||||
@@ -47,3 +47,10 @@ fun Long.toURI(): Uri {
|
||||
this
|
||||
)
|
||||
}
|
||||
|
||||
fun Long.toAlbumArtURI(): Uri {
|
||||
return ContentUris.withAppendedId(
|
||||
Uri.parse("content://media/external/audio/albumart"),
|
||||
this
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package org.oxycblt.auxio.music.models
|
||||
|
||||
import android.graphics.Bitmap
|
||||
|
||||
// Abstraction for Song
|
||||
data class Album(
|
||||
val id: Long = 0L,
|
||||
val title: String = "",
|
||||
val artistName: String = "",
|
||||
val cover: Bitmap? = null,
|
||||
val year: Int = 0,
|
||||
var numSongs: Int = 0
|
||||
) {
|
||||
|
||||
@@ -3,6 +3,10 @@ package org.oxycblt.auxio.music.processing
|
||||
import android.app.Application
|
||||
import android.content.ContentResolver
|
||||
import android.database.Cursor
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.ImageDecoder
|
||||
import android.os.Build
|
||||
import android.provider.MediaStore
|
||||
import android.provider.MediaStore.Audio.Albums
|
||||
import android.provider.MediaStore.Audio.Artists
|
||||
import android.provider.MediaStore.Audio.Genres
|
||||
@@ -13,6 +17,7 @@ import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
import org.oxycblt.auxio.music.models.Genre
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
import org.oxycblt.auxio.music.toAlbumArtURI
|
||||
|
||||
enum class MusicLoaderResponse {
|
||||
DONE, FAILURE, NO_MUSIC
|
||||
@@ -188,10 +193,23 @@ class MusicLoader(private val app: Application) {
|
||||
val year = cursor.getInt(yearIndex)
|
||||
val numSongs = cursor.getInt(numIndex)
|
||||
|
||||
val artUri = id.toAlbumArtURI()
|
||||
var cover: Bitmap? = null
|
||||
|
||||
// Get the album art through either ImageDecoder or MediaStore depending on the
|
||||
// version.
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
cover = ImageDecoder.decodeBitmap(
|
||||
ImageDecoder.createSource(resolver, artUri)
|
||||
)
|
||||
} else {
|
||||
cover = MediaStore.Images.Media.getBitmap(resolver, artUri)
|
||||
}
|
||||
|
||||
albums.add(
|
||||
Album(
|
||||
id, name, artist,
|
||||
year, numSongs
|
||||
cover, year, numSongs
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -17,5 +17,14 @@
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:title="@string/title_library_fragment" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/testAlbum"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</layout>
|
||||
Reference in New Issue
Block a user