Skip to content

Instantly share code, notes, and snippets.

@Kashif-E
Created September 23, 2021 11:11
Show Gist options
  • Save Kashif-E/c262c6ab6a0cd0f1032a80cd4bb14bca to your computer and use it in GitHub Desktop.
Save Kashif-E/c262c6ab6a0cd0f1032a80cd4bb14bca to your computer and use it in GitHub Desktop.
fun shareImages(
contextActibity: Activity,
listOfImages: List<String>,
description: String? = null,
price: String? = null,
name: String? = "",
details: String = "",
catalogName: String = ""
) {
Log.e("list", listOfImages.toString())
val context = MarkazApp.getInstance().baseContext
val cachePath = File(context.cacheDir, "images")
cachePath.mkdirs()
val uriList: ArrayList<Uri> = ArrayList()
val intent = Intent(Intent.ACTION_SEND_MULTIPLE)
intent.type = "image/*"
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
GlobalScope.launch {
listOfImages.forEach {
val loader = ImageLoader(context)
val request = ImageRequest.Builder(context)
.data(
it
)
.allowHardware(false)
.build()
when (loader.execute(request)) {
is SuccessResult -> {
val result = (loader.execute(request) as SuccessResult).drawable
val bitmap = (result as BitmapDrawable).bitmap
val UUID = it
val stream = FileOutputStream("$cachePath/${UUID}.jpeg")
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
val newFile = File(cachePath, "$UUID.jpeg")
val contentUri: Uri = FileProvider.getUriForFile(
context,
"${BuildConfig.APPLICATION_ID}.provider",
newFile
)
uriList.add(contentUri)
}
}
}
}.invokeOnCompletion {
description?.let {
intent.putExtra(
Intent.EXTRA_TEXT,
"*Catalog Name*: $catalogName \n \n*Product Name*: $name \n \n*Product Description*: $description\n \n*Product Details*:\n \n$details \n "
)
}
price?.let {
intent.putExtra(
Intent.EXTRA_TEXT,
"$description\n Price = $price"
)
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList)
try {
contextActibity.startActivity(
Intent.createChooser(
intent,
"Choose the app you want to share on"
)
)
} catch (e: Exception) {
Log.e(
"e",
e.toString() + " " + e.cause + " " + e.message + " " + e.localizedMessage
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment