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

Camera

The document describes an Android application that allows users to take photos using the device camera. It includes the app manifest and main activity code to request camera permissions, launch the camera intent on a button click, and display the captured photo.

Uploaded by

Ritesh Kolate
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)
38 views

Camera

The document describes an Android application that allows users to take photos using the device camera. It includes the app manifest and main activity code to request camera permissions, launch the camera intent on a button click, and display the captured photo.

Uploaded by

Ritesh Kolate
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/ 3

camera

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


<LinearLayout <manifest
xmlns:android="http://schemas.android.com/apk xmlns:android="http://schemas.android.com/apk/res/android"
/res/android" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res- tools:ignore="ExtraText"
auto" package="com.example.camera">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" <uses-feature
android:layout_height="match_parent" android:name="android.hardware.camera"
android:orientation="vertical" android:required="false" />
tools:context=".MainActivity" <uses-permission
tools:ignore="ExtraText"> android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
<ImageView
android:id="@+id/imageView" android:dataExtractionRules="@xml/data_extraction_rules"
android:layout_width="fill_parent" android:fullBackupContent="@xml/backup_rules"
android:layout_height="403dp" android:icon="@mipmap/ic_launcher"
/> android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
<Button android:supportsRtl="true"
android:id="@+id/button" android:theme="@style/Theme.Camera"
android:layout_width="wrap_content" tools:targetApi="31">
android:layout_height="wrap_content" <activity
android:layout_gravity="center" android:name=".MainActivity"
android:text="Click Here to Capture Photo" android:exported="true">
/> <intent-filter>
<action
</LinearLayout> android:name="android.intent.action.MAIN" />

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

</manifest>
camera
package com.example.camera;

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 = findViewById(R.id.button);
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);
}
});
}

@Override
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");
camera
imageView.setImageBitmap(bitmap);
}
}

public void EnableRuntimePermission() {


if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.CAMERA)) {
Toast.makeText(MainActivity.this, "CAMERA permission allows us to access the
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) {
super.onRequestPermissionsResult(RC, per, 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;
}
}
}

You might also like