Mohammad Taqi Velani
Mohammad Taqi Velani

Reputation: 171

How to cut status bar OR actionbar from a bitmap?

  bitmap?.let {
                            cropimg = Bitmap.createBitmap(
                                it,
                                0,
                                56+28,//each mobile has different height of actionbar and status bar
                                it.width,
                                it.height -(56+28)
                            )
                        }

I am taking Screenshot of mobile screen and I am getting bitmap as a result

I want to crop the status bar or navigation bar from that bitmap How to do that? I try above solution but it is not accurate.

Upvotes: 0

Views: 105

Answers (1)

Rezwan
Rezwan

Reputation: 231

Step 1: Get the dynamic height of the status bar

fun getStatusBarHeight(context: Context): Int {
    val resources: Resources = context.resources
    val resourceId: Int = resources.getIdentifier("status_bar_height", "dimen", "android")
    return if (resourceId > 0) resources.getDimensionPixelSize(resourceId) else 0
}

Step 2: Point out the starting point of your new bitmap like this

          val statusbarHeight = getStatusBarHeight(this)

           bitmap?.let {
                            cropimg = Bitmap.createBitmap(
                                it,
                                0,
                                0 + statusbarHeight,
                                it.width,
                                it.height
                            )
                        }

Happy Conding! :)

Upvotes: 0

Related Questions