0% found this document useful (0 votes)
11 views9 pages

Practical No 23

The document contains two Android application components: one for capturing and displaying images using the device's camera, and another for managing Bluetooth functionalities. The first part includes an XML layout and Java code for capturing an image and displaying it in an ImageView. The second part features an XML layout and Java code for turning Bluetooth on/off, listing paired devices, and making the device discoverable.

Uploaded by

Vaman Kulkarni
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)
11 views9 pages

Practical No 23

The document contains two Android application components: one for capturing and displaying images using the device's camera, and another for managing Bluetooth functionalities. The first part includes an XML layout and Java code for capturing an image and displaying it in an ImageView. The second part features an XML layout and Java code for turning Bluetooth on/off, listing paired devices, and making the device discoverable.

Uploaded by

Vaman Kulkarni
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/ 9

Exp.

23

#MainActivity.xmlfile

<!-- res/layout/activity_main.xml -->

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

<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"

android:text="Capture Image" />

<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"

android:contentDescription="Captured Image" />

</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;

public class MainActivity extends Activity {

private static final int CAMERA_REQUEST_CODE = 1;

private ImageView capturedImageView;

private Button captureButton;

private Uri imageUri;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate (savedInstanceState);

setContentView(R.layout.activity_main);

captureButton = findViewById(R.id.capture_button);

capturedImageView = findViewById(R.id.captured_image);

captureButton.setOnClickListener(v -> openCamera());

// Opens the camera to capture an image

private void openCamera() {

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

// Check if there's a camera activity to handle the intent

if (cameraIntent.resolveActivity(getPackageManager()) != null) {

// Create a file to store the photo (you can define your own directory here)

File photoFile = new File(getExternalFilesDir(null), "captured_image.jpg");

imageUri = Uri.fromFile(photoFile);

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);

} else {

Toast.makeText(this, "No camera app found", Toast.LENGTH_SHORT).show();

// Handles the result after capturing the image

@Override

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {


try {

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);

capturedImageView.setImageBitmap(bitmap);

} catch (IOException e) {

e.printStackTrace();

Toast.makeText(this, "Failed to load image", Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(this, "Image capture failed", Toast.LENGTH_SHORT).show();

Output:
Exp.24

#activity_main.xml

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

<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"

android:text="Turn On Bluetooth" />

<Button

android:id="@+id/btnTurnOff"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Turn Off Bluetooth" />

<Button

android:id="@+id/btnListDevices"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="List Paired Devices" />

<Button
android:id="@+id/btnMakeVisible"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Make Bluetooth Visible" />

<TextView

android:id="@+id/tvDeviceList"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Device List will be shown here"

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 {

private BluetoothAdapter bluetoothAdapter;

private TextView tvDeviceList;

private final int REQUEST_ENABLE_BT = 1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

tvDeviceList = findViewById(R.id.tvDeviceList);

if (bluetoothAdapter == null) {

Toast.makeText(this, "Bluetooth is not supported on this device",


Toast.LENGTH_SHORT).show();

return;

Button btnTurnOn = findViewById(R.id.btnTurnOn);

Button btnTurnOff = findViewById(R.id.btnTurnOff);

Button btnListDevices = findViewById(R.id.btnListDevices);

Button btnMakeVisible = findViewById(R.id.btnMakeVisible);

// Turn on Bluetooth

btnTurnOn.setOnClickListener(v -> {

if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

} else {

Toast.makeText(this, "Bluetooth is already on", Toast.LENGTH_SHORT).show();

});

// Turn off Bluetooth

btnTurnOff.setOnClickListener(v -> {

if (bluetoothAdapter.isEnabled()) {

bluetoothAdapter.disable();

Toast.makeText(this, "Bluetooth turned off", Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(this, "Bluetooth is already off", Toast.LENGTH_SHORT).show();

});

// List paired devices

btnListDevices.setOnClickListener(v -> listPairedDevices());

// Make Bluetooth visible

btnMakeVisible.setOnClickListener(v -> makeBluetoothVisible());

private void listPairedDevices() {

if (bluetoothAdapter.isEnabled()) {

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

if (pairedDevices.size() > 0) {

StringBuilder deviceList = new StringBuilder();


for (BluetoothDevice device : pairedDevices) {

deviceList.append(device.getName()).append("\n");

tvDeviceList.setText(deviceList.toString());

} else {

tvDeviceList.setText("No paired devices found.");

} else {

Toast.makeText(this, "Please turn on Bluetooth first", Toast.LENGTH_SHORT).show();

private void makeBluetoothVisible() {

if (bluetoothAdapter.isEnabled()) {

Intent discoverableIntent = new


Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);

startActivity(discoverableIntent);

} else {

Toast.makeText(this, "Please turn on Bluetooth first", Toast.LENGTH_SHORT).show();

You might also like