Skip to content

Commit

Permalink
Add try-catch to try and handle a possible crash when trying to decod…
Browse files Browse the repository at this point in the history
…e image preview

Fixes #18
  • Loading branch information
MateusRodCosta committed Mar 18, 2024
1 parent fdef0ab commit 0414882
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.graphics.BitmapFactory
import android.net.Uri
import android.provider.DocumentsContract
import android.provider.OpenableColumns
import android.util.Log
import com.mateusrodcosta.apps.share2storage.model.UriData
import java.io.*

Expand All @@ -49,13 +50,17 @@ fun getUriData(contentResolver: ContentResolver, uri: Uri?): UriData? {
}

var bitmap: Bitmap? = null

val fileDescriptor = contentResolver.openFileDescriptor(uri, "r")
if (fileDescriptor != null) {
val fd = fileDescriptor.fileDescriptor
bitmap = BitmapFactory.decodeFileDescriptor(fd)
try {
val fileDescriptor = contentResolver.openFileDescriptor(uri, "r")
if (fileDescriptor != null) {
val fd = fileDescriptor.fileDescriptor
bitmap = BitmapFactory.decodeFileDescriptor(fd)
}
fileDescriptor?.close()
} catch (e: Exception) {
Log.w("getUriData] bitmap", e.toString())
bitmap = null
}
fileDescriptor?.close()

return UriData(displayName, type, size, previewImage = bitmap)
}
Expand Down

0 comments on commit 0414882

Please sign in to comment.