startActivityForResult Depreciated Solution | Android Studio | Java

How to use ActivityResult OR ActivityResultLauncher?

Since the startActivityForResult() used to launce the intents like Gallery, Camera, File Pick, Share data, etc is deprecated so now there is a new way to do that ActivityResultLauncher class. 

>>Check for Kotlin

Video Tutorial:

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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <!--ImageView: set image after picking from gallery-->
    <ImageView
        android:id="@+id/profileIv"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/ic_image_black"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>

    <!--Button: launch intent to pick image from gallery-->
    <Button
        android:id="@+id/intentBtn"
        android:text="Pick Image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>


MainActivity.java

package com.technifysoft.activityresultjava;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //UI Views
    private ImageView profileIv;
    private Button intentBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //init UI Views (better to use ViewBinding instead of findViewById)
        profileIv = findViewById(R.id.profileIv);
        intentBtn = findViewById(R.id.intentBtn);

        //handle click, launch intent to pick image from gallery
        intentBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //intent to pick image from gallery
                Intent intent = new Intent(Intent.ACTION_PICK);
                //set type
                intent.setType("image/*");
                galleryActivityResultLauncher.launch(intent);
            }
        });
    }

    private ActivityResultLauncher<Intent> galleryActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    //here we will handle the result of our intent
                    if (result.getResultCode() == Activity.RESULT_OK){
                        //image picked
                        //get uri of image
                        Intent data = result.getData();
                        Uri imageUri = data.getData();

                        profileIv.setImageURI(imageUri);
                    }
                    else {
                        //cancelled
                        Toast.makeText(MainActivity.this, "Cancelled...", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    );

}


Screenshots

How to use ActivityResult OR ActivityResultLauncher?


Comments

Popular posts from this blog

Manage External Storage Permission | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Java

Add a Back Button to Action Bar Android Studio (Kotlin)