Skip to content

Commit

Permalink
Use extension function for creating bitmap from vector drawables (#2984)
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored Jan 6, 2025
1 parent c661d94 commit 4c9a5f5
Showing 1 changed file with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,41 @@ package com.google.android.ground.ui.util
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.ImageDecoder
import android.net.Uri
import android.os.Build
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

class BitmapUtil @Inject internal constructor(@ApplicationContext private val context: Context) {

/** Retrieves an image for the given url as a [Bitmap]. */
fun fromUri(url: Uri?): Bitmap {
require(url != null) { "Uri cannot be null" }

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
/**
* Retrieves an image for the given URI as a [Bitmap].
*
* @param url The URI of the image.
* @return The decoded [Bitmap].
* @throws IllegalArgumentException If the URI cannot be opened or decoded.
*/
fun fromUri(url: Uri): Bitmap =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, url))
} else {
// Use InputStream and BitmapFactory for older APIs
context.contentResolver.openInputStream(url)?.use { inputStream ->
BitmapFactory.decodeStream(inputStream)
} ?: throw IllegalArgumentException("Unable to open URI: $url")
context.contentResolver.openInputStream(url)?.use { BitmapFactory.decodeStream(it) }
?: throw IllegalArgumentException("Unable to open URI: $url")
}
}

fun fromVector(resId: Int): Bitmap {
val vectorDrawable = ContextCompat.getDrawable(context, resId)!!

// Specify a bounding rectangle for the Drawable.
vectorDrawable.setBounds(0, 0, vectorDrawable.intrinsicWidth, vectorDrawable.intrinsicHeight)
val bitmap =
Bitmap.createBitmap(
vectorDrawable.intrinsicWidth,
vectorDrawable.intrinsicHeight,
Bitmap.Config.ARGB_8888,
)
val canvas = Canvas(bitmap)
vectorDrawable.draw(canvas)
return bitmap
}
/**
* Retrieves an image for the given vector resource as a [Bitmap].
*
* @param resId The resource ID of the vector drawable.
* @return The decoded [Bitmap], or null if the resource is not found.
* @throws IllegalArgumentException If the resource is not a vector drawable.
*/
fun fromVector(@DrawableRes resId: Int): Bitmap =
ContextCompat.getDrawable(context, resId)?.toBitmap()
?: throw IllegalArgumentException("Resource not found: $resId")
}

0 comments on commit 4c9a5f5

Please sign in to comment.