Skip to content

Commit

Permalink
#1250 fix: capture FileNotFoundException when picking screenshot for …
Browse files Browse the repository at this point in the history
…actions
  • Loading branch information
sds100 committed Aug 4, 2024
1 parent e5d1ba7 commit 83a10ea
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package io.github.sds100.keymapper.actions.pinchscreen

import android.annotation.SuppressLint
import android.graphics.ImageDecoder
import android.graphics.Bitmap
import android.graphics.Point
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.core.graphics.decodeBitmap
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
Expand Down Expand Up @@ -47,15 +44,13 @@ class PinchPickDisplayCoordinateFragment : Fragment() {
}

private val screenshotLauncher =
registerForActivityResult(ActivityResultContracts.GetContent()) {
it ?: return@registerForActivityResult

val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.createSource(requireContext().contentResolver, it)
.decodeBitmap { _, _ -> }
} else {
MediaStore.Images.Media.getBitmap(requireContext().contentResolver, it)
}
registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
uri ?: return@registerForActivityResult

val bitmap: Bitmap? =
FileUtils.decodeBitmapFromUri(requireContext().contentResolver, uri)

bitmap ?: return@registerForActivityResult

val displaySize = Point().apply {
@Suppress("DEPRECATION")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package io.github.sds100.keymapper.actions.swipescreen

import android.annotation.SuppressLint
import android.graphics.ImageDecoder
import android.graphics.Bitmap
import android.graphics.Point
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.core.graphics.decodeBitmap
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
Expand Down Expand Up @@ -43,15 +40,13 @@ class SwipePickDisplayCoordinateFragment : Fragment() {
}

private val screenshotLauncher =
registerForActivityResult(ActivityResultContracts.GetContent()) {
it ?: return@registerForActivityResult

val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.createSource(requireContext().contentResolver, it)
.decodeBitmap { _, _ -> }
} else {
MediaStore.Images.Media.getBitmap(requireContext().contentResolver, it)
}
registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
uri ?: return@registerForActivityResult

val bitmap: Bitmap? =
FileUtils.decodeBitmapFromUri(requireContext().contentResolver, uri)

bitmap ?: return@registerForActivityResult

val displaySize = Point().apply {
@Suppress("DEPRECATION")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package io.github.sds100.keymapper.actions.tapscreen

import android.annotation.SuppressLint
import android.graphics.ImageDecoder
import android.graphics.Bitmap
import android.graphics.Point
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.core.graphics.decodeBitmap
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
Expand Down Expand Up @@ -50,12 +47,10 @@ class PickDisplayCoordinateFragment : Fragment() {
registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
uri ?: return@registerForActivityResult

val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.createSource(requireContext().contentResolver, uri)
.decodeBitmap { _, _ -> }
} else {
MediaStore.Images.Media.getBitmap(requireContext().contentResolver, uri)
}
val bitmap: Bitmap? =
FileUtils.decodeBitmapFromUri(requireContext().contentResolver, uri)

bitmap ?: return@registerForActivityResult

val displaySize = Point().apply {
@Suppress("DEPRECATION")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package io.github.sds100.keymapper.system.files

import android.annotation.SuppressLint
import android.content.ContentResolver
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import androidx.core.graphics.decodeBitmap
import java.io.FileNotFoundException
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Calendar

Expand All @@ -23,4 +32,21 @@ object FileUtils {

return format.format(date)
}

fun decodeBitmapFromUri(contentResolver: ContentResolver, uri: Uri): Bitmap? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return ImageDecoder.createSource(contentResolver, uri)
.decodeBitmap { _, _ -> }
} else {
try {
@Suppress("DEPRECATION")
return MediaStore.Images.Media.getBitmap(contentResolver, uri)
} catch (_: FileNotFoundException) {
// do nothing. The user just picked a file, how can it not exist?
return null
} catch (_: IOException) {
return null
}
}
}
}

0 comments on commit 83a10ea

Please sign in to comment.