Practical No 23
Practical No 23
23
#MainActivity.xmlfile
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/capture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<ImageView
android:id="@+id/captured_image"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="16dp"
android:src="@android:drawable/ic_menu_camera"
</LinearLayout>
#MainActivity.java
package com.example.captureanddisplayimage;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@Override
setContentView(R.layout.activity_main);
captureButton = findViewById(R.id.capture_button);
capturedImageView = findViewById(R.id.captured_image);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
// Create a file to store the photo (you can define your own directory here)
imageUri = Uri.fromFile(photoFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
} else {
@Override
capturedImageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
} else {
Output:
Exp.24
#activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/btnTurnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:id="@+id/btnTurnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:id="@+id/btnListDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:id="@+id/btnMakeVisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TextView
android:id="@+id/tvDeviceList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp" />
</LinearLayout>
#MainActivity.java
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Set;
public class MainActivity extends Activity {
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
tvDeviceList = findViewById(R.id.tvDeviceList);
if (bluetoothAdapter == null) {
return;
// Turn on Bluetooth
btnTurnOn.setOnClickListener(v -> {
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
});
btnTurnOff.setOnClickListener(v -> {
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.disable();
} else {
});
if (bluetoothAdapter.isEnabled()) {
if (pairedDevices.size() > 0) {
deviceList.append(device.getName()).append("\n");
tvDeviceList.setText(deviceList.toString());
} else {
} else {
if (bluetoothAdapter.isEnabled()) {
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
} else {