How to share image from jetpack composer - android

Hey I'm new to kotlin and I'm having problem with this. The images are in the drawable folder. And I need to share the image in jetpack composer. When I click now I can only see the text but not the image to share.
Image(
painter = painterResource(id = image.imgId),
contentDescription = "Test",
modifier = Modifier.fillMaxWidth()
)
var pickedImageUri by remember { mutableStateOf<Uri?>(null) }
val context = LocalContext.current
val intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, pickedImageUri)
putExtra(Intent.EXTRA_TEXT, "Description of Image")
type = "image/*"
}
val shareIntent = Intent.createChooser(intent, "Share")
Button( onClick = {
//val description = "Image sent"
context.startActivity(shareIntent)
}) {
Text("Share")
}

Related

How to select multiple photos from gallery using jetpack compose?

In my project i want to display the paths of gallery photos using lazy column in jetpack compose but when i select any photos and return back to screen , lazy column dost not show any paths. This my code
#Composable
fun ItemScreen(){
Column(modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(10.dp)) {
Headline(modifier = Modifier.fillMaxWidth())
AddItemRow(titleName = "Enter Color", KeyboardOptions(keyboardType = KeyboardType.Text))
OpenGallery()
}
#Composable
fun OpenGallery(){
var selectedImage = remember { mutableStateListOf<Uri?>() }
val launcher = rememberLauncherForActivityResult(contract = ActivityResultContracts.GetMultipleContents()){
selectedImage = it.toMutableStateList()
}
GalleryContent(selectedImage) {
launcher.launch("image/jpeg")
}
}
#Composable
fun GalleryContent(
selectedImage: MutableList<Uri?> ?= null,
OnImageClick: () -> Unit
)
{
if (selectedImage?.isEmpty() == true) {
Button(
onClick = OnImageClick,
) {
Text(text = "Choose Photos", color = Color.White, style = MaterialTheme.typography.bodySmall)
}
}
else{
LazyColumn(modifier = Modifier.height(300.dp)) {
items(selectedImage!!){ it ->
Text(text = it?.path.toString(), color = Color.Black)
Spacer(modifier = Modifier.height(9.dp))
}
}
}
}
How do i fix it . Please help
Instead of selectedImage = it.toMutableStateList() you can use:
val selectedImage = remember { mutableStateListOf<Uri?>(null) }
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) {
selectedImage.apply {
clear()
addAll(it)
}
}

Show image with Coil and Uri in Android

I want to show picture with Coil from Uri.
Following code shows picture but when I choose one.
I want to store Uri and when Screen starts i want to show it immediately.
val uri = "content://com.android.providers.media.documents/document/image%3A18".toUri()
var imageUri by remember { mutableStateOf<Uri?>(null) }
val context = LocalContext.current
val launcher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.GetContent()) {
uri: Uri? ->
imageUri = uri
}
Column {
Button(onClick = {
//here we are going to add logic for picking image
launcher.launch(
"image/*"
)
}, content = {
Text(text = "Select Image From Gallery")
})
AsyncImage(
model = ImageRequest.Builder(context = context)
.data(imageUri)
.crossfade(true)
.build(),
contentDescription = "",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(144.dp)
)
Image(
painter = rememberAsyncImagePainter(imageUri),
contentDescription = "Picture",
)
val uri is uri I chooshed before and I copied it and put to string to show image.
But this way first I need to choose picture and it will show. If I put uri instead of imageUri in .data() or rememberAsyncImagePainter nothing happens.

Get image uri and display it with Coil in Compose (Android)

I'm trying to fetch image uri and then display it in my application with coil.
var imageUri by remember { mutableStateOf<Uri?>(null) }
val launcher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.GetContent()) { uri: Uri? ->
imageUri = uri
}
Column {
Button(onClick = {
//here we are going to add logic for picking image
launcher.launch(
"image/*"
)
}, content = {
Text(text = "Select Image From Gallery")
})
Log.d(TAG, "ClientCardItem: $imageUri")
Text(text = imageUri.toString())
I made this just to check if i fetched uri. I got result like this:
"content://com.android.providers.media.documents/document/image%3A18", i put it to string.
This is for image display:
Image(
painter = rememberImagePainter(
data = Uri.parse(uri) // or ht
)
,
contentDescription = "123",
modifier = Modifier.height(200.dp).width(200.dp),
contentScale = ContentScale.FillWidth
)
I know that I dont have .jpg or .png or something else to display it properly. My question is how to get that format to display picture I picked.
EDIT:
rememberAsyncImagePainter, rememberImagePainter or AsyncImage are not found ?
After I updated to:
implementation "io.coil-kt:coil:2.2.2"
On AsyncImage() -> Unresolved reference: AsyncImage
To display the image just use:
AsyncImage(model = imageUri, contentDescription = "Picture")
or:
Image(
painter = rememberAsyncImagePainter(imageUri),
contentDescription = "Picture",
)

Convert Jetpack Compose Image into BitMap Image for Sharing Purpose

I am looking for help on how to share image binary content from my PhotoApp to external Social Media apps.
I load the image from an API using Coil and display the image in a composable.
#Composable
fun PhotoDetailsScreen( photo: AstroPhoto ... ) {
val context = LocalContext.current
val imgUri = photo.url.toUri()
.buildUpon()
.scheme("https")
.build()
Column ( ...
//Image
Image(
painter = rememberImagePainter(
data = imgUri,
builder = {
crossfade(true)
placeholder(R.drawable.loading_animation)
}
) ...
//Assist Chip
AssistChip(onClick = { shareAstroPhoto("", context) }
Clicking on the AssistChip calls the below shareAstroPhoto() fxn that takes the uri pointing to the image file to fire an ACTION_SEND Intent
fun shareAstroPhoto(uri:String, context: Context){
val intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uri)
type = "image/jpg"
}
context.startActivity(Intent.createChooser(intent, null))
}
I intend to get a Bitmap out of my composable, save the Bitmap in my own ContentProvider or MediaStore (I know how these 2 work) and then pass the uri of the saved BitMap to Intent.EXTRA_STREAM.
Have browsed through similar cases and videos but all I find is working with XML code.
Therefore my query is how to convert Jetpack Compose Image from a composable into a Bitmap to enable sharing the file with other external apps through Android Sharesheet.
I'm not sure if I understand your question. If you just want to share the image, then:
Util function:
fun Context.shareImage(title: String, image: Drawable, filename: String) {
val file = try {
val outputFile = File(cacheDir, "$filename.png")
val outPutStream = FileOutputStream(outputFile)
image.toBitmap().compress(CompressFormat.PNG, 100, outPutStream)
outPutStream.flush()
outPutStream.close()
outputFile
} catch (e: Throwable) {
return toast(e)
}
val uri = file.toUriCompat(this)
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND
type = "image/png"
putExtra(Intent.EXTRA_STREAM, uri)
}
startActivity(Intent.createChooser(shareIntent, title))
}
Share image with (Coil 2.0):
val painter = rememberAsyncImagePainter(
ImageRequest.Builder(LocalContext.current)
.data(url)
.build()
)
Button(
onClick={
val state = painter.state as? AsyncImagePainter.State.Success
val drawable = state?.result.drawable
context.shareImage(
"Share image via",
drawable,
"filename"
)
}
)
Updated
Those are small helper functions, but if you want them:
fun File.toUriCompat(context: Context): Uri {
return FileProvider.getUriForFile(context, context.packageName + ".provider", this)
}
fun Context.toast(throwable: Throwable) =
throwable.message?.let { toast(it) }
?: toast(R.string.unknown_error)
fun Context.toast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
You can get drawable using painter in jetpack compose, then use that Drawble to share..
#Composable
fun photoItem(
photoUrl: String,
modifier: Modifier = Modifier,
contentScale: ContentScale = ContentScale.Fit
): Drawable? {
val painter = rememberAsyncImagePainter(
Builder(LocalContext.current)
.placeholder(drawable.placeholder)
.data(photoUrl)
.build()
)
Image(
contentScale = contentScale,
painter = painter,
contentDescription = null,
modifier = modifier,
)
val state = painter.state as? AsyncImagePainter.State.Success
return state?.result?.drawable
}
then cast the Drawable to BitmapDrawable

Sharesheet in Jetpack Compose

How do I share content using Android's Sharesheet in Jetpack Compose?
Inside a #Composable you can use an Intent in a standard way.
Something like:
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
val context = LocalContext.current
Button(onClick = {
context.startActivity(shareIntent)
}){
Text("Share")
}

Categories

Resources