I'm working on an Android application and I've run into a strange issue. I have two classes, GoogleSignUpFragment
and EmailSignUpFragment
, which both contain identical pieces of code for picking an image from the gallery and loading it into a ShapeableImageView using Glide.
The code in both classes works as follows:
- An
Intent
is created to pick an image from the gallery. - In
onActivityResult()
, the Uri of the chosen image is retrieved. - Glide is used to load the image into a
ShapeableImageView
, with a circle crop applied. - There are methods to save temporary the image in order to load it into Firebase Storage.
During the debugging process, I've noticed that in EmailSignUpFragment
everything runs smoothly. After the line requestBuilder.into(target)
, execution moves to override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {/* ... */}
as expected.
However, in GoogleSignUpFragment
, after the line requestBuilder.into(target)
, the execution jumps to override fun onLoadCleared(placeholder: Drawable?) {/* ... */}
, and immediately afterwards the execution of the fragment stops.
This is the code that is identical in the two classes
private fun onPropicClick() {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, PICK_IMAGE_REQUEST)
}
@Deprecated("Deprecated in Java")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null) {
selectedImageUri = data.data!!
loadImageWithCircularCrop(selectedImageUri)
}
}
private fun loadImageWithCircularCrop(imageUri: Uri?) {
val requestBuilder = Glide.with(this)
.asBitmap()
.load(imageUri)
.apply(RequestOptions.circleCropTransform())
val target = object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
croppedImageFile = saveBitmapToFile(resource)
propicImageView.setImageBitmap(resource)
}
override fun onLoadCleared(placeholder: Drawable?) {
propicImageView.setImageDrawable(null)
}
}
requestBuilder.into(target)
}
private fun saveBitmapToFile(bitmap: Bitmap): File {
val file = File(requireContext().cacheDir, "propic.png")
val outputStream = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
outputStream.flush()
outputStream.close()
return file
}
In the OnCreateView
method of the Fragment i Have this line of code that starts the intent to choose the photo:
propicImageView.setOnClickListener { onPropicClick() }
- I've tried debugging both classes line by line, examining the execution flow. Given the identical code in both classes, I expected them to behave the same way
- I've also verified that my app has the necessary permissions to read external storage, and the same code works perfectly in the other class.
- I've tried using
if(!isAdded)
to check if the Fragment was not destroyed. - I've tried using a ViewModel.
- I've tried using
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
. - I've tried this code with API 30 and API 32.