In android, An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
What we are going to build in this article?
In this article, we will learn that how we can pass Video using Intent between two activities and then play that video in the second activity. Here is a sample video of what we are going to build in this application. Note that we are going to implement this application using Java language.
Step by Step Implementation
Step 1: Create a New Project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- You can change the name of the project at your convenience.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2: Adding storage permission
Follow the path app > manifests > AndroidManifest.xml and paste the following piece of code in it.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Step 3: Working on XML files
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 version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/bt_select"
android:text = "Select Video"
android:layout_centerInParent = "true"
/>
</ RelativeLayout >
|
Follow the path app > right click > new > activity > Empty Activity > name it as “MainActvity2”. Navigate to the app > res > layout > activity_main2.xml and add the below code to that file. Below is the code for the activity_main2.xml file.
<? xml version = "1.0" encoding = "utf-8" ?>
< androidx.constraintlayout.widget.ConstraintLayout android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity2" >
< RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:clipToPadding = "false"
android:fitsSystemWindows = "true" >
< VideoView
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:id = "@+id/video_view" />
</ RelativeLayout >
</ androidx.constraintlayout.widget.ConstraintLayout >
|
Step 4: Working on java files
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.
package com.example.videointent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// Initialize variables
Button btSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Assign variable
btSelect=findViewById(R.id.bt_select);
btSelect.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
// Check condition
if (ContextCompat.checkSelfPermission(MainActivity. this , Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
// When permission is not granted
// Request permission
ActivityCompat.requestPermissions(MainActivity. this , new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}
, 1 );
}
else
{
// When permission is granted
// Create method
selectVideo();
}
}
});
}
private void selectVideo() {
// Initialize intent
Intent intent= new Intent(Intent.ACTION_PICK);
// set type
intent.setType( "video/*" );
// start activity result
startActivityForResult(Intent.createChooser(intent, "Select Video" ), 100 );
}
@Override
public void onRequestPermissionsResult( int requestCode, String[] permissions, int [] grantResults) {
super .onRequestPermissionsResult(requestCode, permissions, grantResults);
// Check condition
if (requestCode== 1 && grantResults.length > 0 && grantResults[ 0 ]
== PackageManager.PERMISSION_GRANTED)
{
// When permission is granted
// Call method
selectVideo();
}
else
{
// When permission is denied
// Display toast
Toast.makeText(getApplicationContext()
, "Permission denied" ,Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
super .onActivityResult(requestCode, resultCode, data);
// Check condition
if (requestCode== 100 && resultCode==RESULT_OK && data!= null )
{
// When result code is okay
// Initialize uri
Uri uri=data.getData();
// Initialize intent
Intent intent= new Intent( this ,MainActivity2. class );
// Put extra
intent.putExtra( "uri" ,uri.toString());
// Start activity
startActivity(intent);
}
}
} |
Go to the MainActivity2.java file and refer to the following code. Below is the code for the MainActivity2.java file. Comments are added inside the code to understand the code in more detail.
package com.example.videointent;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;
public class MainActivity2 extends AppCompatActivity {
// Initialize variables
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// Assign variables
videoView=findViewById(R.id.video_view);
// Get data from main activity
Bundle bundle=getIntent().getExtras();
// Check condition
if (bundle!= null )
{
// When bundle not equal to null
// Initialize uri
Uri uri=Uri.parse(bundle.getString( "uri" ));
// Set video uri
videoView.setVideoURI(uri);
// Start video
videoView.start();
}
}
} |
Here is the final output of our application.
Output: