0% found this document useful (0 votes)
40 views

Camera and Wifi Code

The document contains code for a camera application. The XML layout file defines views for a text label, image view, and button. The Java code gets references to these views, launches the camera on button click, and displays the captured photo by setting it to the image view. It also includes code to save the photo to the device gallery.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Camera and Wifi Code

The document contains code for a camera application. The XML layout file defines views for a text label, image view, and button. The Java code gets references to these views, launches the camera on button click, and displays the captured photo by setting it to the image view. It also includes code to save the photo to the device gallery.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Camera code

Xml file
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="Deprecated">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="101dp"
android:layout_y="37dp"
android:text="Camera Example"
android:textSize="27dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText,SpUsage,TextSizeCheck" />

<ImageView
android:id="@+id/imgv"
android:layout_width="237dp"
android:layout_height="292dp"
android:layout_x="75dp"
android:layout_y="124dp"
tools:ignore="ContentDescription" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="139dp"
android:layout_y="530dp"
android:text="Camera"
android:textSize="20dp"
tools:ignore="HardcodedText,SpUsage,TextSizeCheck" />

</AbsoluteLayout>

Java file
package com.example.camera;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


ImageView igw3;
Button btn;
int REQ_CODE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
igw3 = findViewById(R.id.imgv);
btn = findViewById(R.id.bt1);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent ie = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(ie, REQ_CODE);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE) {
Bitmap bt = (Bitmap) data.getExtras().get("data");
igw3.setImageBitmap(bt);
saveImageToGallery(bt);
}
}

private void saveImageToGallery(Bitmap bitmap) {


String savedImageURL = MediaStore.Images.Media.insertImage(
getContentResolver(),
bitmap,
"Image",
"Image captured from Camera"
);
if (savedImageURL != null) {
Toast.makeText(this, "Image saved to gallery",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Failed to save image to gallery",
Toast.LENGTH_SHORT).show();
}
}
}

output
Wifi Code

Xml file
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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:ignore="Deprecated">

<Button
android:id="@+id/btnToggleWiFi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="100dp"
android:text="Toggle WiFi"
tools:ignore="HardcodedText" />

</AbsoluteLayout>

Java file
package com.example.wifiexp;

import android.Manifest;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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

import com.example.wifiexp.R;

public class MainActivity extends AppCompatActivity {


private static final int PERMISSION_REQUEST_CODE = 1;
private WifiManager wifiManager;

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

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


wifiManager = (WifiManager)
getApplicationContext().getSystemService(WIFI_SERVICE);

btnToggleWiFi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleWiFi();
}
});

// Check and request necessary permissions


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CHANGE_WIFI_STATE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.CHANGE_WIFI_STATE}, PERMISSION_REQUEST_CODE);
}
}
}

private void toggleWiFi() {


if (wifiManager != null) {
wifiManager.setWifiEnabled(true);
Toast.makeText(this, "WiFi turned on",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "WiFi manager not found",
Toast.LENGTH_SHORT).show();
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted. You can now turn
on WiFi.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission denied. WiFi cannot be
turned on.", Toast.LENGTH_SHORT).show();
}
}
}
}

manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

Output:

You might also like