Skip to content

Commit

Permalink
Change to use Dispatchers.IO in BitmapCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
jja08111 committed Aug 30, 2024
1 parent cfd381e commit 24c17ec
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class BitmapCreator @Inject constructor(
@ApplicationContext private val context: Context,
) {
fun create(imageUri: Uri): Bitmap {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, imageUri))
} else {
@Suppress("DEPRECATION")
MediaStore.Images.Media.getBitmap(context.contentResolver, imageUri)
suspend fun create(imageUri: Uri): Bitmap =
withContext(Dispatchers.IO) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, imageUri))
} else {
@Suppress("DEPRECATION")
MediaStore.Images.Media.getBitmap(context.contentResolver, imageUri)
}
}
}
}

0 comments on commit 24c17ec

Please sign in to comment.