Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Resize Images Programmatically in Android?

  • Last Updated : 17 Sep, 2021

There are many applications available on the net for performing image operations such as cropping, reducing image file size, or resizing the images to particular dimensions. Most of these applications are API based where the image is uploaded to anonymous servers, various functions are performed and then displayed as a result available for download. The process becomes complex with the involvement of the internet. However, there are few applications that can locally perform similar operations.

Through this article, we will show you how you could resize an image to custom dimensions programmatically in Android.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add two Buttons to upload and resize and an ImageView to display the image when uploaded and resized in the layout.


<?xml version="1.0" encoding="utf-8"?>
    <!-- Button to upload Image from the device -->
    <!-- Button to perform resize operation -->
        android:layout_alignParentEnd="true" />
    <!-- ImageView to display images after
          uploading and resizing -->



Step 3: Working with the MainActivity.kt file


Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail. Per

resizing operations in the main code.



import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.annotation.RequiresApi
class MainActivity : AppCompatActivity() {
    // Declaring ImageView, number of images to pick
    // from the device and a Bitmap to store the image
    private lateinit var mImageView: ImageView
    private val mPickImage = 1
    private lateinit var mYourBitmap: Bitmap
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and initializing the buttons
        val mUploadButton = findViewById<Button>(
        val mResizeButton = findViewById<Button>(
        // Initializing the image view
        mImageView = findViewById(
        // When upload button is clicked, the intent navigates
        // to the local content in the device,
        // where one can select the desired image
        mUploadButton.setOnClickListener {
            val intent = Intent(Intent.ACTION_GET_CONTENT)
            intent.type = "image/*"
            startActivityForResult(intent, mPickImage)
        // When resize button is clicked
        mResizeButton.setOnClickListener {
            // Generate a new Bitmap of custom dimensions and set it in the image view
            val resized = Bitmap.createScaledBitmap(mYourBitmap, 300, 300, true)
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == mPickImage && resultCode == Activity.RESULT_OK) {
            // If the image file does not exist
            if (data == null) {
            // Otherwise
            try {
                // Load the image address and set it in the image view
                val imageUri: Uri =!!
                val source = ImageDecoder.createSource(this.contentResolver, imageUri)
                mYourBitmap = ImageDecoder.decodeBitmap(source)
            } catch (e: IOException) {





You can see that the image size is reduced when resize button is clicked.



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!