We are trying to add the thumbnail of a youtube video and link it to the youtube app through code. We have synced all implementations to the build.grade. (app) file but the imports just don't work and show up in red. Is it possible we are using old imports that just don't work anymore?
We have tried looking up the imports and it did not result in anything. We tried retyping imports instead of copy pasting it into the IDE which worked before but it does not work now. Theres more errors within the code but I believe it is because of the imports not going through so some of these tools will not work.
Implementations added to build.grade (app)
implementation 'com.google.api-client:google-api-client:1.31.3'
implementation 'com.google.apis:google-api-services-youtube:v3-rev20220225-1.31.3'
implementation 'com.github.bumptech.glide:glide:4.12.0'
Code for main-activity
import android.content.Intentimport android.net.Uriimport android.os.Bundleimport android.widget.ImageViewimport androidx.appcompat.app.AppCompatActivityimport com.bumptech.glide.Glideimport com.google.api.client.googleapis.javanet.GoogleNetHttpTransportimport com.google.api.client.json.JsonFactoryimport com.google.api.client.json.jackson2.JacksonFactoryimport com.google.api.services.youtube.YouTubeimport com.google.api.services.youtube.model.SearchListResponseimport com.google.api.services.youtube.model.SearchResultimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.GlobalScopeimport kotlinx.coroutines.launchimport kotlinx.coroutines.withContextclass MainActivity : AppCompatActivity() { private lateinit var imageView: ImageView private lateinit var youtube: YouTube override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) imageView = findViewById(R.id.imageView) // Initialize YouTube API client youtube = YouTube.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), null ).setApplicationName(getString(R.string.app_name)).build() // Load the thumbnail of the video with the given ID loadThumbnail("VIDEO_ID_HERE") } private fun loadThumbnail(videoId: String) { GlobalScope.launch { val thumbnailUrl = getThumbnailUrl(videoId) withContext(Dispatchers.Main) { // Load thumbnail into ImageView using Glide library Glide.with(this@MainActivity) .load(thumbnailUrl) .into(imageView) // Set click listener on ImageView to open the YouTube app imageView.setOnClickListener { val intent = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:$videoId")) intent.putExtra("VIDEO_ID", videoId) startActivity(intent) } } } } private suspend fun getThumbnailUrl(videoId: String): String { // Search for the video with the given ID val searchResponse = youtube.search().list("id").apply { q = videoId type = "video" fields = "items(id(videoId))" maxResults = 1 }.execute() // Get the first search result, which should be the video we're looking for val searchResult: SearchResult = (searchResponse.items as List<SearchResult>)[0] // Get the URL of the default thumbnail for the video return "https://img.youtube.com/vi/${searchResult.id.videoId}/default.jpg" }}