Pick an Image from the Gallery – Android Studio - Java
How to Pick an Image from the Gallery?
In this tutorial, we will walk through the complete process of selecting an image from the device’s Gallery in an Android application. This feature is commonly used in many modern apps—for uploading profile pictures, choosing photos for posts, or attaching images to forms.
We will start by creating a simple layout with a button that the user can tap to open the Gallery. Once the button is clicked, the system’s built-in image picker will appear, allowing the user to browse their photo library and select a single image. After the user chooses an image, we will retrieve its URI and display it inside an ImageView within our app.
>> Check for Kotlin
>> Check for Compose
Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:padding="50dp" tools:context=".MainActivity"> <ImageView android:id="@+id/imageIv" android:layout_width="match_parent" android:layout_height="300dp" android:adjustViewBounds="true" android:scaleType="fitCenter" android:src="@drawable/ic_image_black" /> <com.google.android.material.button.MaterialButton android:id="@+id/pickImageBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pick Image Gallery" /> </LinearLayout>
MainActivity.java
package com.technifysoft.myapplication; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.technifysoft.myapplication.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; private static final String TAG = "MAIN_TAG"; private Uri imageUri = null; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); binding.pickImageBtn.setOnClickListener(v -> pickImageGallery()); } private void pickImageGallery() { Log.d(TAG, "pickImageGallery: "); Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); galleryActivityResultLauncher.launch(intent); } private final ActivityResultLauncher<Intent> galleryActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { //Check if image is picked or not if (result.getResultCode() == RESULT_OK) { //Image Picked from Gallery, get data/intent from ActivityResult Intent data = result.getData(); if (data != null) { //get uri of image picked imageUri = data.getData(); Log.d(TAG, "onActivityResult: Image Picked From Gallery: " + imageUri); try { //set to profileIv binding.imageIv.setImageURI(imageUri); } catch (Exception e) { Log.e(TAG, "onActivityResult error: ", e); } } } else { //Cancelled Log.d(TAG, "onActivityResult: Cancelled..."); Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show(); } }); }

Comments
Post a Comment