How to Pick Images From Gallery Android Studio

Explore seamless image selection in your Android app with our Gallery Picker feature in Java.


Serial Number Description
1 ActivityResultLauncher pickImages

ActivityResultLauncher pickImages: This line initializes a variable named pickImages with the type ActivityResultLauncher that takes an Intent as a parameter. This variable is used to register and launch an activity for result.

2 registerForActivityResult

registerForActivityResult: This method is used to register a callback for handling the result of an activity started for a result. It takes in an ActivityResultContract and an ActivityResultCallback.

3 new ActivityResultContracts.StartActivityForResult()

new ActivityResultContracts.StartActivityForResult(): This part specifies the type of activity to be started for result. In this case, it's using StartActivityForResult contract, which is typically used to start another activity and get a result back from it.

4 new ActivityResultCallback() { ... }

new ActivityResultCallback() { ... }: This section creates a new instance of ActivityResultCallback for handling the result of the activity. Inside the onActivityResult method, the result of the activity is processed.

5 Uri imageUri = result.getData().getData()

Uri imageUri = result.getData().getData(): This line attempts to extract the URI of the selected image from the result data.

6 imageView.setImageURI(imageUri)

imageView.setImageURI(imageUri): If the image URI is successfully obtained, it is set to an imageView to display the selected image.

7 Toast.makeText(MainActivity.this,"No Image Selected", Toast.LENGTH_SHORT).show()

Toast.makeText(MainActivity.this,"No Image Selected", Toast.LENGTH_SHORT).show(): If an exception occurs during the attempt to get the image URI, a toast message is displayed indicating that no image was selected.

8 Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)

This line creates a new intent with the action Intent.ACTION_PICK, indicating that the intent is used to pick an item. In this case, it specifies that it will pick an image. The second parameter, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, specifies the URI for accessing images in the external storage.

9 pickImages.launch(intent)

pickImages.launch(intent): This line launches the activity associated with the pickImages ActivityResultLauncher, passing in the intent created in the previous step. This effectively starts the image selection activity and registers the pickImages launcher to handle the result once the activity finishes.


Xml Code activity_main.xml

   <ImageView  
     android:id="@+id/imageView"  
     android:layout_width="match_parent"  
     android:layout_height="300dp"  
     android:scaleType="centerCrop"  
     />  
   
   <Button  
     android:id="@+id/pickImage"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="Pick Images"  
     />  

Declare variable in MainActivity.java

   ImageView imageView;  
   Button pickImage;  

Pick Image onCreate method

     // Initialize  
     imageView = findViewById(R.id.imageView);  
     pickImage = findViewById(R.id.pickImage);  
   
     pickImage.setOnClickListener(view -> {  
       Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
       pickImages.launch(intent);  
     });  

Create a function in MainActivity.java

   ActivityResultLauncher<Intent> pickImages = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),  
       new ActivityResultCallback<ActivityResult>() {  
         @Override  
         public void onActivityResult(ActivityResult result) {  
           try {  
             Uri imageUri = result.getData().getData();  
             imageView.setImageURI(imageUri);  
           }catch (Exception e){  
             Toast.makeText(MainActivity.this,"No Image Selected", Toast.LENGTH_SHORT).show();  
           }  
         }  
       }  
   );  

Video:

Next Post Previous Post
No Comment
Add Comment
comment url