Professional Documents
Culture Documents
MAD Prac 23
MAD Prac 23
Exercise:
1]Program:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// By ID we can get each component which id is assigned in XML file get
Buttons and imageview.
camera_open_id = findViewById(R.id.camera_button);
click_image_id = findViewById(R.id.click_image);
Output:
2]Program:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Video"
android:onClick="captureVideo"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Video"
android:onClick="playVideo"
android:layout_marginTop="16dp"/>
</LinearLayout>
MainActivity.java:
package com.example.mad;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == VIDEO_REQUEST && resultCode == RESULT_OK) {
videoUri = data.getData();
}
}
}
VideoPlayActivity:
package com.example.mad;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play);
mVideoView = findViewById(R.id.videoView);
String videoUriString = getIntent().getStringExtra("videoUri");
if (videoUriString != null) {
Uri videoUri = Uri.parse(videoUriString);
mVideoView.setVideoURI(videoUri);
mVideoView.start();
}
}
}
activity_video_play:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VideoPlayActivity">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Output: