When i use the cropping action on the image in my app it firstly saves it in the gallery at the first instance when i only touch the crop option.After touching the crop option for 32 times and successfully saving image in gallery it starts going back to the main activity when i touch it 33rd time.It does the same behaviour until i delete one image from the gallery, where it has saved those images. After that it again works for 32 clicks but after 32 it shows same behaviour. I have used the library ucrop by ytlantis.
This code is under the onclicklistner for the crop button and i want that the image should not get saved in the gallery while i touch the crop button and this 32 times touch should get resolved please help somebody.
val crop: FloatingActionButton = findViewById(R.id.crop)
crop.setOnClickListener {
fun getBitmapAsByteArray(bitmap: Bitmap): ByteArray {
val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream)
return outputStream.toByteArray()
}
val options = UCrop.Options()
options.setCompressionFormat(Bitmap.CompressFormat.PNG)
Glide.with(this)
.asBitmap()
.load("$image_url")
.into(object : CustomTarget<Bitmap>() {
@RequiresApi(Build.VERSION_CODES.N)
override fun onResourceReady(
resource: Bitmap,
transition: com.bumptech.glide.request.transition.Transition<in Bitmap>?
) {
val inputStream = ByteArrayInputStream(getBitmapAsByteArray(resource))
val byteArrayUri = Uri.parse(
MediaStore.Images.Media.insertImage(
contentResolver,
BitmapFactory.decodeStream(inputStream),
"Title",
null
)
)
val destinationUri = Uri.fromFile(File(cacheDir, "cropped"))
val uCrop = UCrop.of(byteArrayUri, destinationUri)
.withOptions(options)
startActivityForResult(
uCrop.getIntent(this@image_full_size_avtivity),
UCrop.REQUEST_CROP
)
val originalFile = File(cacheDir, "original_image.png")
originalFile.delete()
val croppedFile = File(cacheDir, "cropped.png")
croppedFile.delete()
}
override fun onLoadCleared(placeholder: Drawable?) {}
})
}
}
@RequiresApi(Build.VERSION_CODES.N)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
val resultUri = UCrop.getOutput(data!!)
val wallpaperManager = WallpaperManager.getInstance(this)
val inputStream = resultUri?.let { contentResolver.openInputStream(it) }
val bitmap = BitmapFactory.decodeStream(inputStream)
wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK)
}
super.onActivityResult(requestCode, resultCode, data)
}
}