0% found this document useful (0 votes)
10 views7 pages

Practical No. 23

The document outlines two Android applications: one for capturing and displaying images using an ImageView, and another for recording and playing videos using a VideoView. Both applications include XML layouts and Java code to handle user interactions and permissions for camera and audio functionalities. The code demonstrates the use of ActivityResultLauncher for image capture and intent handling for video recording.

Uploaded by

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

Practical No. 23

The document outlines two Android applications: one for capturing and displaying images using an ImageView, and another for recording and playing videos using a VideoView. Both applications include XML layouts and Java code to handle user interactions and permissions for camera and audio functionalities. The code demonstrates the use of ActivityResultLauncher for image capture and intent handling for video recording.

Uploaded by

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

Q. Write a program to capture an image and import android.graphics.

Bitmap;
display it using image view. import android.os.Bundle;
import android.provider.MediaStore;
activity_main.xml import android.view.View;
import android.widget.Button;
<?xml version="1.0" encoding="utf-8"?> import android.widget.ImageView;
<LinearLayout import
xmlns:android="http://schemas.android.com/ap androidx.activity.result.ActivityResultLauncher
k/res/android" ;
android:layout_width="match_parent" import
android:layout_height="match_parent" androidx.activity.result.contract.ActivityResult
android:orientation="vertical" Contracts;
android:padding="16dp"> import
androidx.appcompat.app.AppCompatActivity;
<!-- Button to capture an image -->
<Button public class MainActivity extends
android:id="@+id/captureButton" AppCompatActivity {
android:layout_width="wrap_content"
android:layout_height="wrap_content" private ImageView imageView; // To display
android:textSize="30sp" captured image
android:text="Capture Image" /> private Button captureButton; // Button to
capture image
<!-- ImageView to display captured image --
> @Override
<ImageView protected void onCreate(Bundle
android:id="@+id/imageView" savedInstanceState) {
android:layout_width="300dp" super.onCreate(savedInstanceState);
android:layout_height="300dp" setContentView(R.layout.activity_main);
android:layout_marginTop="16dp"
android:adjustViewBounds="true" // Initialize UI components
android:scaleType="fitCenter" imageView =
android:contentDescription="Captured findViewById(R.id.imageView);
Image" /> captureButton =
</LinearLayout> findViewById(R.id.captureButton);

MainActivity.java // ActivityResultLauncher to handle the


result of the camera intent
package com.example.practicalno_23; ActivityResultLauncher<Intent>
captureImageLauncher =
registerForActivityResult(
import android.content.Intent;
new <uses-permission
ActivityResultContracts.StartActivityForResult android:name="android.permission.CAMERA"
(), />
result -> { <uses-permission
if (result.getResultCode() == android:name="android.permission.RECORD_
RESULT_OK && result.getData() != null) { AUDIO" />
Bitmap imageBitmap = (Bitmap)
result.getData().getExtras().get("data"); <application
android:allowBackup="true"
imageView.setImageBitmap(imageBitmap); // android:icon="@mipmap/ic_launcher"
Display captured image android:label="@string/app_name"
}
}); android:roundIcon="@mipmap/ic_launcher_ro
und"
// Set click listener for the capture button android:supportsRtl="true"
captureButton.setOnClickListener(v -> { android:theme="@style/Theme.App">
Intent takePictureIntent = new
Intent(MediaStore.ACTION_IMAGE_CAPTU <activity
RE); android:name=".MainActivity"
if android:exported="true">
(takePictureIntent.resolveActivity(getPackage <intent-filter>
Manager()) != null) { <action
android:name="android.intent.action.MAIN" />
captureImageLauncher.launch(takePictureIntent <category
); android:name="android.intent.category.LAUN
} CHER" />
}); </intent-filter>
} </activity>
} </application>

AndroidManifest.xml </manifest>

<?xml version="1.0" encoding="utf-8"?>


<manifest
xmlns:android="http://schemas.android.com/ap
k/res/android">

<!-- Permissions for camera and audio


recording -->
Q. Write a program to record a video using
various camera methods

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/ap
k/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Button to start recording -->


<Button
android:id="@+id/recordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Record Video"
android:layout_centerInParent="true" />

<!-- VideoView to display the recorded video


-->
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:layout_above="@id/recordButton"
android:layout_marginBottom="16dp" />
</RelativeLayout>

MainActivity.java

package com.example.practicalno_23_1;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore; recordButton.setOnClickListener(v ->
import android.widget.Button; askCameraPermission());
import android.widget.Toast; }
import android.widget.VideoView;
// Method to check and request camera and
import androidx.annotation.NonNull; audio permissions
import private void askCameraPermission() {
androidx.appcompat.app.AppCompatActivity; if
import androidx.core.app.ActivityCompat; (ContextCompat.checkSelfPermission(this,
import androidx.core.content.ContextCompat; Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED ||
public class MainActivity extends
AppCompatActivity { ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO) !=
private static final int PackageManager.PERMISSION_GRANTED)
VIDEO_REQUEST_CODE = 101; // Request {
code for video recording
private static final int ActivityCompat.requestPermissions(this,
CAMERA_PERMISSION_CODE = 102; // new String[]
Request code for camera permission {Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO},
private Button recordButton; // Button to
start recording CAMERA_PERMISSION_CODE);
private VideoView videoView; // VideoView } else {
to display the recorded video recordVideo(); // If permissions are
private Uri videoUri; // URI to store the granted, start recording video
recorded video }
}
@Override
protected void onCreate(Bundle @Override
savedInstanceState) { public void onRequestPermissionsResult(int
super.onCreate(savedInstanceState); requestCode, @NonNull String[] permissions,
setContentView(R.layout.activity_main); @NonNull int[] grantResults) {

// Initialize UI components super.onRequestPermissionsResult(requestCod


recordButton = e, permissions, grantResults);
findViewById(R.id.recordButton);
videoView = if (requestCode ==
findViewById(R.id.videoView); CAMERA_PERMISSION_CODE) {
if (grantResults.length > 0 &&
// Set click listener for the record button grantResults[0] ==
PackageManager.PERMISSION_GRANTED) Toast.makeText(this, "Failed to Record
{ Video", Toast.LENGTH_SHORT).show();
recordVideo(); // Permission granted, }
start recording video }
} else { }
Toast.makeText(this, "Camera
Permission Denied", AndroidManifest.xml
Toast.LENGTH_SHORT).show();
} <?xml version="1.0" encoding="utf-8"?>
} <manifest
} xmlns:android="http://schemas.android.com/ap
k/res/android">
// Method to start the video recording intent
private void recordVideo() { <!-- Permissions for camera and audio
Intent videoIntent = new recording -->
Intent(MediaStore.ACTION_VIDEO_CAPTU <uses-permission
RE); android:name="android.permission.CAMERA"
if />
(videoIntent.resolveActivity(getPackageManag <uses-permission
er()) != null) { android:name="android.permission.RECORD_
startActivityForResult(videoIntent, AUDIO" />
VIDEO_REQUEST_CODE);
} <application
} android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@Override android:label="@string/app_name"
protected void onActivityResult(int
requestCode, int resultCode, Intent data) { android:roundIcon="@mipmap/ic_launcher_ro
super.onActivityResult(requestCode, und"
resultCode, data); android:supportsRtl="true"

if (requestCode == android:theme="@style/Theme.PracticalNo_23
VIDEO_REQUEST_CODE && resultCode == _1">
RESULT_OK && data != null) {
// Get the URI of the recorded video and <activity
display it in the VideoView android:name=".MainActivity"
videoUri = data.getData(); android:exported="true">
videoView.setVideoURI(videoUri); <intent-filter>
videoView.start(); // Start playing the <action
recorded video android:name="android.intent.action.MAIN" />
} else {
<category
android:name="android.intent.category.LAUN
CHER" />
</intent-filter>
</activity>
</application>

</manifest>

You might also like