-
Notifications
You must be signed in to change notification settings - Fork 143
Description
Hello! First of all, thank you for the amazing library. I want to share the current displayed image to another app or maybe social media. But I read that this library couldn't do that yet. Does anyone have any idea how?
Below is my current implementation but relying in Glide doesn't sound amazing...
binding.ivLogDetailImage.setItemChangeListener(object : ItemChangeListener {
override fun onItemChanged(position: Int) {
imagePosition = position
Glide.with(this@LogDetailActivity)
.asBitmap()
.load(getActivityData.imageDetail?.get(imagePosition))
.placeholder(R.drawable.image_not_available)
.error(R.drawable.image_not_available)
.into(
object : CustomTarget<Bitmap?>() {
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap?>?
) {
mImgBitmap = resource
}
override fun onLoadCleared(placeholder: Drawable?) {}
})
Log.e(TAG, "position : $imagePosition")
}
})
binding.btShareLogDetail.setOnClickListener {
CoroutineScope(Dispatchers.IO).launch {
compressBitmapAndShare(mImgBitmap, getActivityData?.activityTitle)
}
}
private suspend fun compressBitmapAndShare(bitmap: Bitmap, shareText: String?) {
val currentTimeMillis = System.currentTimeMillis()
val title = "Image_${
SimpleDateFormat("yyyy-MM-dd_HH.mm.ss", Locale.getDefault()).format(currentTimeMillis)
}"
withContext(Dispatchers.IO) {
val bytes = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, title)
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
put(MediaStore.Images.Media.IS_PENDING, 1)
}
val resolver = contentResolver
val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
uri?.let { imageUri ->
resolver.openOutputStream(imageUri)?.use { outputStream ->
outputStream.write(bytes.toByteArray())
}
values.clear()
values.put(MediaStore.Images.Media.IS_PENDING, 0)
resolver.update(imageUri, values, null, null)
shareImage(imageUri, shareText)
}
}
}
private fun shareImage(imageUri: Uri, shareText: String?) {
val share = Intent(Intent.ACTION_SEND).apply {
type = "image/*"
putExtra(Intent.EXTRA_STREAM, imageUri)
putExtra(Intent.EXTRA_TEXT, shareText ?: "")
startActivity(Intent.createChooser(this, "Share via"))
}
startActivity(share)
}