0% found this document useful (0 votes)
238 views2 pages

Practical No 23

This document contains code for an Android application that allows capturing and displaying images and videos from the device camera. The activity_main.xml layout contains an ImageView or VideoView for displaying media. The MainActivity code launches the device camera on a button click, receives the result, and displays the captured image or starts playing the captured video in the corresponding view. The application demonstrates capturing and displaying images and videos from the device camera on button click in Android.

Uploaded by

atharvabutte03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views2 pages

Practical No 23

This document contains code for an Android application that allows capturing and displaying images and videos from the device camera. The activity_main.xml layout contains an ImageView or VideoView for displaying media. The MainActivity code launches the device camera on a button click, receives the result, and displays the captured image or starts playing the captured video in the corresponding view. The application demonstrates capturing and displaying images and videos from the device camera on button click in Android.

Uploaded by

atharvabutte03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical No 23

Q1.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="412dp"
android:layout_height="743dp"
android:orientation="vertical"
android:padding="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="401dp" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Click image" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical23;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity {


private final int req_Code = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button) findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v) {
Intent cam_intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(cam_intent,req_Code);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
if (requestCode == req_Code){
Bitmap b_img = (Bitmap) data.getExtras().get("data");
((ImageView) findViewById(R.id.img)).setImageBitmap(b_img);
}
}
}
}

Q2.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="412dp"
android:layout_height="743dp"
android:orientation="vertical"
android:padding="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<VideoView
android:id="@+id/vid"
android:layout_width="match_parent"
android:layout_height="401dp" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Start Video" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical23;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button)findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent cam_intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(cam_intent, 6);
} }); }

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
if (requestCode == 6) {
((VideoView) findViewById(R.id.vid)).setVideoURI(data.getData());
((VideoView) findViewById(R.id.vid)).start();
} } }

You might also like