In this article, we are going to send an image from one activity to another. We will be using putExtra to send image and we will be using bundle to get the data sent from the previous activity and then we will be showing the received image.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
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.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:gravity = "center"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< Button
android:id = "@+id/sendimagei"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:padding = "16dp"
android:text = "Send Image"
android:textColor = "#000"
android:textSize = "32sp" />
</ LinearLayout >
|
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button sendimage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendimage = findViewById(R.id.sendimagei);
sendimage.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sendimage = new Intent(MainActivity. this , SecondActivity. class );
sendimage.putExtra( "image" , R.drawable.circle);
startActivity(sendimage);
}
});
}
}
|
Step 4: Create another new Activity
Refer to this article and name the activity as SecondActivity. Go to the activity_second.xml file and refer to the following code. Below is the code for the activity_second.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:gravity = "center"
android:orientation = "vertical"
tools:context = ".SecondActivity" >
< ImageView
android:id = "@+id/images"
android:layout_width = "250dp"
android:layout_height = "250dp" />
</ LinearLayout >
|
Step 5: Working with the SecondActivity.java file
Go to the SecondActivity.java file and refer to the following code. Below is the code for the SecondActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
ImageView imageView;
int imagevalue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
imageView = findViewById(R.id.images);
Bundle bundle = getIntent().getExtras();
if (bundle != null ) {
imagevalue = bundle.getInt( "image" );
}
imageView.setImageResource(imagevalue);
}
}
|
Output:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 Apr, 2021
Like Article
Save Article