Catch exceptions if the file cannot be decoded.

This commit is contained in:
onurays 2020-03-25 18:39:35 +03:00
parent 6130a0a654
commit 5db1010e47
1 changed files with 10 additions and 5 deletions

View File

@ -27,13 +27,18 @@ import androidx.exifinterface.media.ExifInterface
object ImageUtils { object ImageUtils {
fun getBitmap(context: Context, uri: Uri): Bitmap? { fun getBitmap(context: Context, uri: Uri): Bitmap? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, uri)) ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, uri))
} else { } else {
context.contentResolver.openInputStream(uri)?.use { inputStream -> context.contentResolver.openInputStream(uri)?.use { inputStream ->
BitmapFactory.decodeStream(inputStream) BitmapFactory.decodeStream(inputStream)
} }
} }
} catch (e: Exception) {
e.printStackTrace()
null
}
} }
fun getOrientation(context: Context, uri: Uri): Int { fun getOrientation(context: Context, uri: Uri): Int {