Camera Application

 MainActivity -


  

package com.example.cameraapplication;


import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

import androidx.core.content.ContextCompat;


import android.Manifest;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.pm.PackageManager;

import android.graphics.Bitmap;

import android.os.Build;

import android.os.Bundle;

import android.provider.MediaStore;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.Toast;


import java.io.IOException;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {



    ImageView imageView;

    Button camera, setWall;

    Bitmap bitmap;


    private static final int PERMISSION_REQUEST_CODE = 100;

    private static final int CAMERA_REQUEST_CODE = 1;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getSupportActionBar().hide();

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);


        imageView = findViewById(R.id.imageView);

        camera = findViewById(R.id.buttonCamera);

        setWall = findViewById(R.id.buttonSetWallpaper);


        camera.setOnClickListener(this);

        setWall.setOnClickListener(this);

    }


    @Override

    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.buttonCamera:


                if (checkPermission()) {

                    //write your main code to execute, It will execute if the permission is already given.

                    openCameraMethod();

                } else {

                    requestPermission();

                }


                break;


            case R.id.buttonSetWallpaper:

                try {

                    getApplicationContext().setWallpaper(bitmap);

                    Toast.makeText(getApplicationContext(), "Wallpaper Set", Toast.LENGTH_LONG).show();

                    finish();

                } catch (IOException e) {

                    e.printStackTrace();

                }


                Thread thread = new Thread() {

                    @Override

                    public void run() {

                        try {

                            sleep(2000);

                        } catch (Exception e) {

                            e.printStackTrace();

                        } finally {

                            startActivity(new Intent(MainActivity.this, MainActivity.class));

                        }

                    }

                };

                thread.start();


                break;

        }

    }


    public void openCameraMethod() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(intent, CAMERA_REQUEST_CODE);

    }


    private boolean checkPermission() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)

                != PackageManager.PERMISSION_GRANTED) {

            // Permission is not granted

            return false;

        }

        return true;

    }


    private void requestPermission() {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},

                PERMISSION_REQUEST_CODE);

    }


    @Override

    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

        switch (requestCode) {

            case PERMISSION_REQUEST_CODE:

                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(getApplicationContext(), "Camera Permission Granted", Toast.LENGTH_SHORT).show();

                    openCameraMethod();

                    // main logic

                } else {

                    Toast.makeText(getApplicationContext(), "Camera Permission Denied", Toast.LENGTH_SHORT).show();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)

                                != PackageManager.PERMISSION_GRANTED) {

                            showMessageOKCancel("You need to allow access permissions for Camera",

                                    new DialogInterface.OnClickListener() {

                                        @Override

                                        public void onClick(DialogInterface dialog, int which) {

                                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                                                requestPermission();

                                            }

                                        }

                                    });

                        }

                    }

                }

                break;

        }

    }


    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {

        new AlertDialog.Builder(MainActivity.this)

                .setMessage(message)

                .setPositiveButton("OK", okListener)

                .setNegativeButton("Cancel", null)

                .create()

                .show();

    }


    @Override

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

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            Bundle b = data.getExtras();

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

            imageView.setImageBitmap(bitmap);

        }

    }

}


-----------------------------------------------------------------------------------------------------------------------------


activity_main -


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

    android:orientation="vertical"

    tools:context=".MainActivity">



    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="90dp"

        android:orientation="horizontal">


        <Button

            android:id="@+id/buttonCamera"

            android:layout_width="0dp"

            android:layout_height="90dp"

            android:layout_weight="1"

            android:background="#FFFAF0"

            android:text="Open Camera" />


        <Button

            android:id="@+id/buttonSetWallpaper"

            android:layout_width="0dp"

            android:layout_height="90dp"

            android:layout_weight="1"

            android:background="#FFFAF0"

            android:text="Set Wallpaper" />


    </LinearLayout>


    <ImageView

        android:id="@+id/imageView"

        android:layout_width="match_parent"

        android:layout_height="0dp"

        android:layout_weight="1"

        android:scaleType="fitXY"

        android:src="@drawable/img5" />

</LinearLayout>


-----------------------------------------------------------------------------------------------------------------------------


AndroidManifest -


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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.cameraapplication">


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

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


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

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

Comments

Popular posts from this blog

Generate Signing Release keystore

πŸš€ COMPLETE CI/CD FROM SCRATCH