Android Camera App In this tutorial we are going to discuss about How to take camera permission and storage permission in android app and open camera .

Android Camera App this tutorial explain about open camera in android app also we discuss check self permission for camera permission and storage permission in android app

1. Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from templates

2. Now open AndroidManifest.xml file and add required permission to access camera and storage


          <uses-permission android:name="android.permission.CAMERA" />
          <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
          <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.technxtcodelabs.storagecameraaccess">
          <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
          <uses-permission android:name="android.permission.CAMERA" />
          <uses-permission android:name="android.permission.INTERNET" />
          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
          <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
          
          <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>

3. Now we create a button and an image view to our layout file i.e. activity_main.xml of our Main_Activity.java file or just implement the following xml code.

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="9"/>
<Button
    android:id="@+id/btnUpload"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Upload" />

activity_main.xml


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

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"/>

<Button
android:id="@+id/btnUpload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Upload" />

</LinearLayout>

4. Now finally open your Main_Activity.java file and implement click Listener to your onCreate method and open your camera using intent inside the click Listener , also we check self permission for camera and storage to access the camera app and  external storage of the device.

Main_Activity.java

package com.example.technxtcodelabs.storagecameraaccess;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
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 {

    ImageView img;
    Button btnUpload;
    private final int CAMERA_REQUEST_CODE=2;
    private final int STORAGE_REQUEST_CODE=5;

    Integer SELECT_FILE=0;
    Integer REQUEST_CAMERA=1;

    final CharSequence[] option={"Camera","Gallery","Cancel"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img=(ImageView)findViewById(R.id.img);
        btnUpload=(Button)findViewById(R.id.btnUpload);

        askPermission(Manifest.permission.READ_EXTERNAL_STORAGE,STORAGE_REQUEST_CODE);
        askPermission(Manifest.permission.CAMERA,CAMERA_REQUEST_CODE);


        btnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("Choose option");
                dialog.setItems(option, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(option[which].equals("Camera")){
                            Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(intent,REQUEST_CAMERA);

                        }else if (option[which].equals("Gallery")){
                            Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            intent.setType("image/*");
                            startActivityForResult(intent.createChooser(intent,"Select file"),SELECT_FILE);

                        }else if (option[which].equals("Cancel")){
                            dialog.dismiss();

                        }
                    }
                });
                dialog.show();
            }
        });


    }

    private void askPermission(String permission,int requestCode) {
        if (ContextCompat.checkSelfPermission(this,permission)!= PackageManager.PERMISSION_GRANTED){
            // We Dont have permission
            ActivityCompat.requestPermissions(this,new String[]{permission},requestCode);


        }else {
            // We already have permission do what you want
            Toast.makeText(MainActivity.this,"Permission is already granted.",Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case CAMERA_REQUEST_CODE:
                if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(this,"Camera Permission Granted",Toast.LENGTH_LONG).show();

                }else{
                    Toast.makeText(this,"Camera Permission Denied",Toast.LENGTH_LONG).show();
                }
                break;
            case STORAGE_REQUEST_CODE:
                if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(this,"Storage Permission Granted",Toast.LENGTH_LONG).show();

                }else{
                    Toast.makeText(this,"Storage Permission Denied",Toast.LENGTH_LONG).show();
                }
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode== Activity.RESULT_OK){
            if(requestCode==REQUEST_CAMERA){
                Bundle bundle=data.getExtras();
                final Bitmap bp=(Bitmap)bundle.get("data");
                img.setScaleType(ImageView.ScaleType.FIT_CENTER);
                img.setImageBitmap(bp);
            }else if(requestCode==SELECT_FILE){
                Uri selectedImage = data.getData();
                img.setImageURI(selectedImage);
            }
        }else if(requestCode==RESULT_CANCELED){
            Toast.makeText(this,"Cancelled",Toast.LENGTH_LONG).show();
        }
    }
}

Now run the project on a mobile or any Emulator

%d bloggers like this: