I have a composable for example
Box(modifier){ ... }
I want to share this view as an image with other apps, that's why I have to convert this Box into an image that can be shared with other apps such as WhatsApp etc. Thank you
CodePudding user response:
Box(
modifier: BoxModifier(
shape: BoxShape.rectangle,
color: Colors.red,
),
child: Image.network(
'https://picsum.photos/250?image=9',
),
);
This code converts the Box into an image that can be shared with other apps. The BoxModifier class provides a way to modify the appearance of a box, and the Image class provides a way to display images.
CodePudding user response:
You can check out this link for how to build your own capturing system
How to screenshot selectively or scrollable Composable?
You basically get view using
val view: View = LocalView.current
then get position of your Composable using
var composableBounds by remember {
mutableStateOf<Rect?>(null)
}
using onGloballyPositionedModifier
Box(modifier = modifier
.onGloballyPositioned {
composableBounds = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
it.boundsInWindow()
} else {
it.boundsInRoot()
}
}
) {
content()
}
Then capturing image using
val bitmap = Bitmap.createBitmap(
width,
height,
Bitmap.Config.ARGB_8888
)
PixelCopy
, this is important, if you don't use this on Oreo and above you are likely to get crash on some devices
PixelCopy.request(
(this.context as Activity).window,
bounds.toAndroidRect(),
bitmap)
or
val canvas = Canvas(bitmap)
.apply {
translate(-bounds.left, -bounds.top)
}
this.draw(canvas)
canvas.setBitmap(null)
bitmapCallback.invoke(ImageResult.Success(bitmap))
i also created a library that takes screenshot or screenshots in specified interval.
https://github.com/SmartToolFactory/Compose-Screenshot
Problem with mine and every other screen capture library out there is not being able to capture scrollable Composables by default.