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

Write A Program To Capture An Image Using Camera and Display It. Activity - Main - XML

The document outlines the implementation of two Android applications: one for capturing images using the camera and displaying them, and another for sending and receiving SMS. It includes XML layout files and Java code for both functionalities, along with an explanation of the need for permissions in Android to protect user data and privacy. Permissions are essential for accessing system features like the camera and Bluetooth, ensuring user consent before accessing sensitive functionalities.

Uploaded by

adityazz603
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)
15 views7 pages

Write A Program To Capture An Image Using Camera and Display It. Activity - Main - XML

The document outlines the implementation of two Android applications: one for capturing images using the camera and displaying them, and another for sending and receiving SMS. It includes XML layout files and Java code for both functionalities, along with an explanation of the need for permissions in Android to protect user data and privacy. Permissions are essential for accessing system features like the camera and Bluetooth, ensuring user consent before accessing sensitive functionalities.

Uploaded by

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

Write a program to capture an image using camera and display it.

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:gravity="center">

<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="10dp"
android:scaleType="centerCrop" />

<Button
android:id="@+id/captureBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Image" />
</LinearLayout>
MainActivity.java (Java)
public class MainActivity extends AppCompatActivity {

ImageView imageView;
Button captureBtn;
ActivityResultLauncher<Intent> cameraLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = findViewById(R.id.imageView);
captureBtn = findViewById(R.id.captureBtn);

cameraLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK) {
Bitmap photo = (Bitmap) result.getData().getExtras().get("data");
imageView.setImageBitmap(photo);
}
});
captureBtn.setOnClickListener(v -> {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraLauncher.launch(intent);
});
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
What is Animation? Explain with example

Develop an application to send and receive SMS.


Sendxml
<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="20dp">
<EditText
android:id="@+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"
android:inputType="phone" />

<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message"
android:inputType="textMultiLine"
android:minLines="3"/>

<Button
android:id="@+id/buttonSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send SMS" />

</LinearLayout>
Sendjava

package com.example.progressbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText phoneNumber, message;
Button sendBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

phoneNumber = findViewById(R.id.editTextPhone);
message = findViewById(R.id.editTextMessage);
sendBtn = findViewById(R.id.buttonSend);

sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phone = phoneNumber.getText().toString();
String msg = message.getText().toString();

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phone, null, msg, null, null);
Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Failed to send SMS",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
receive.java
package com.example.progressbar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class receivesms extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;

if (bundle != null) {
Object[] msg = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[msg.length];

for (int i = 0; i < msgs.length; i++) {


msgs[i] = SmsMessage.createFromPdu((byte[]) msg[i]);
String sender = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();

Toast.makeText(context, "Received from: " + sender + "\nMessage: " + msgBody,


Toast.LENGTH_LONG).show();
}
}
}
}

Discuss the need of permissions in Android. Describe the permissions to set


system functionalities like Bluetooth, camera.
Why Permissions are Needed in Android:
• Permissions are used to protect user data and privacy.
• If an app wants to use features like camera, Bluetooth, location, etc., it
must ask the user first.
• This keeps the device safe and secure from unwanted access.

You might also like