0% found this document useful (0 votes)
24 views6 pages

Exp 23

This document contains code for an Android application that allows a user to capture an image using the device camera. It includes Java code for the main activity class, the Android manifest file, and XML layout code. The main activity class handles launching the camera, receiving the captured image, and displaying it. It also requests runtime permission to access the camera if needed.

Uploaded by

Yash Balotiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Exp 23

This document contains code for an Android application that allows a user to capture an image using the device camera. It includes Java code for the main activity class, the Android manifest file, and XML layout code. The main activity class handles launching the camera, receiving the captured image, and displaying it. It also requests runtime permission to access the camera if needed.

Uploaded by

Yash Balotiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Xml file

1)

JAVA CODE

package com.example.exp23_1;

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

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


Button button ;
ImageView imageView ;
public static final int RequestPermissionCode = 1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);

EnableRuntimePermission();

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent = new


Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, 7);

}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {

super.onActivityResult(requestCode, resultCode, data);


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

Bitmap bitmap = (Bitmap) data.getExtras().get("data");

imageView.setImageBitmap(bitmap);
}
}

public void EnableRuntimePermission(){

if
(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Mani
fest.permission.CAMERA))
{

Toast.makeText(MainActivity.this,"CAMERA permission allows us


to Access CAMERA app", Toast.LENGTH_LONG).show();

} else {

ActivityCompat.requestPermissions(MainActivity.this,new
String[]{Manifest.permission.CAMERA}, RequestPermissionCode);

}
}

@Override
public void onRequestPermissionsResult(int RC, String per[], int[]
PResult) {

switch (RC) {

case RequestPermissionCode:

if (PResult.length > 0 && PResult[0] ==


PackageManager.PERMISSION_GRANTED) {

Toast.makeText(MainActivity.this,"Permission Granted,
Now your application can access CAMERA.", Toast.LENGTH_LONG).show();

} else {

Toast.makeText(MainActivity.this,"Permission Canceled,
Now your application cannot access CAMERA.", Toast.LENGTH_LONG).show();

}
break;
}
}
}

android manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp23_1">

<uses-permission android:name="android.permission.CAMERA" />

<uses-feature
android:name="android.hardware.camera2"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Exp23_1">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>

</manifest>

XML CODE

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


<LinearLayout 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"
android:orientation="vertical">

<ImageView
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:id="@+id/imageView" />

<Button
android:text="Click here to capture image using camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />

</LinearLayout>
Output

You might also like