Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Can someone help me figure out how to display album art in android studio app?

Can someone help me figure out how to display album art in android studio app?

Plan

Premium

Country

US

Device

Pixel 6a

Operating System

windows10

 

My Question or Issue

 

here is the code in my android studio atm. Picasso can display images properly, but I can't get spotify to return the album art and display it on screen. Here is the main code. Please let me know if you have any insights:

 

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.spotify.sdk.android.auth.AuthorizationClient
import com.spotify.sdk.android.auth.AuthorizationRequest
import com.spotify.sdk.android.auth.AuthorizationResponse
import com.squareup.picasso.Picasso
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request.Builder
import okhttp3.Response
import org.json.JSONObject
import java.io.IOException

class MainActivity : AppCompatActivity() {

private lateinit var imageView: ImageView
private val CLIENT_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
private val REDIRECT_URI = "album-art-pulll://callback"
private val REQUEST_CODE = 1337




fun loadImageWithPicasso() {
Picasso.get()

.load("https://www.google.com/images/srpr/logo11w.png")

.into(imageView)

}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

imageView = findViewById(R.id.albumArtImageView)
//loadImageWithPicasso() // this works
authenticateWithSpotify() // this does not //////////////////////////////////////

}

private fun authenticateWithSpotify() {
val builder = AuthorizationRequest.Builder(
CLIENT_ID,
AuthorizationResponse.Type.TOKEN,
REDIRECT_URI
)

builder.setScopes(arrayOf("user-read-playback-state"))
val request = builder.build()

AuthorizationClient.openLoginActivity(this, REQUEST_CODE, request)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
super.onActivityResult(requestCode, resultCode, intent)

if (requestCode == REQUEST_CODE) {
val response = AuthorizationClient.getResponse(resultCode, intent)
if (response.type == AuthorizationResponse.Type.TOKEN) {
// Use the token to fetch the current playing track and display its album art
fetchCurrentPlayingTrack(response.accessToken)
}
}
}
private fun fetchCurrentPlayingTrack(accessToken: String) {
val request = Builder()
.url("https://api.spotify.com/v1/me/player/currently-playing")
.addHeader("Authorization", "Bearer $accessToken")
.build()

OkHttpClient().newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
// Handle the error
}

override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
println("Response not successful: ${response.code}")
}
if (response.isSuccessful) {
val jsonResponse = JSONObject(response.body?.string())

// Extract and log album information
val albumInfo = jsonResponse.getJSONObject("item").getJSONObject("album")
Log.d("Spotify", "Album Info: $albumInfo")

val albumArtUrl = albumInfo.getJSONArray("images").getJSONObject(0).getString("url")

runOnUiThread {
// Use Picasso or Glide to load the image into your ImageView
Picasso.get()
.load(albumArtUrl)
.into(imageView, object : com.squareup.picasso.Callback {
override fun onSuccess() {
Log.d("Picasso", "Spotify image loaded successfully")
Log.d("Spotify", "API Response: ${response.body?.string()}")
}

override fun onError(e: Exception?) {
Log.e("Picasso", "Error loading Spotify image", e)
Log.d("Spotify", "API Response: ${response.body?.string()}")
}
})
}
}
}
})
}

}

 

Reply
1 Reply

@Hubo you replied a few years back and I didn't see it at the time. If you're still around, I'd love some feedback as to what could be going on here.

 

Also, anyone else active on here? Or is there a better place to post questions like these?

Thanks.

Suggested posts