Showing posts with label Importing Image From Camera in android. Show all posts
Showing posts with label Importing Image From Camera in android. Show all posts

June 20, 2018

Capture Image Using Camera

       On this blog I am going to Explain How to capture Image. Here I am using FileProvider for Capture Image. Here is the steps to integrate Image Capture using Camera on your application.

Add Camera Permission

         We need to Add the Camera permission on AndroidManifest.xml file like,


<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


Add File Provider

         We need to Add provider inside the application tag on AndroidManifest.xml file like,

<provider
   android:name="android.support.v4.content.FileProvider"
   android:authorities="${applicationId}.fileprovider"
   android:exported="false"
   android:grantUriPermissions="true">
      <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/file_paths"></meta-data>
</provider>




Add file path

          Inside the  provider tag we added android:resource="@xml/file_paths", we need to add new folder xml(If the folder not exist inside the res folder) and inside the xml folder we need to add new resource file file_paths with the below content,


<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.android.myapplication/files/Pictures" />
</paths>


Java Code

          Here I am capturing the image using my function takePicture(), Before calling this function I am checking the camera permission using my function checkCameraPermissionGranded(), On the Camera Button Click I am using,


if (!checkCameraPermissionGranded())
    requestCameraPermission();
else                    
    takePicture();


Check Camera Permission
private boolean checkCameraPermissionGranded() {
        if (ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else
            return false;
}


Request Camera Permission


      If the permission is not given we will request permission for access camera for our application using requestCameraPermission() methid. Once user grand permission it will automatically execute override method onRequestPermissionsResult. 


    private void requestCameraPermission() {
        ActivityCompat.requestPermissions(HomeActivity.this, new String[]{android.Manifest.permission.CAMERA,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE}, Constants.CAPTURE_REQUEST_CODE);
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == Constants.CAPTURE_REQUEST_CODE) {
            if (checkCameraPermissionGranded())
                takePicture();
            else {
                requestCameraPermission();
                Toast.makeText(HomeActivity.this, "Permission Not Granded", Toast.LENGTH_LONG).show();
            }
        } else if (requestCode == Constants.LOCATION_REQUEST_CODE) {
            if (!checkLocationPermissionGranded()) {
                requestCameraPermission();
                Toast.makeText(HomeActivity.this, "Permission Not Granded", Toast.LENGTH_LONG).show();
            } else
                location = getUserLocation();
        }

    }


Capture Image

  Using takePicture() method we will capture Image , Once capture the image it will execute the override method  onActivityResult




private void takePicture() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
        }
        if (photoFile != null) {
            ProfilePicUri = FileProvider.getUriForFile(HomeActivity.this, "com.android.myapplication.fileprovider", photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, ProfilePicUri);
            startActivityForResult(takePictureIntent, Constants.IMAGE_CAPTURE_CAMERA);
        }
    }
}




@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Constants.IMAGE_CAPTURE_CAMERA && resultCode == RESULT_OK) {
        if (ProfilePicPath != null && ProfilePicPath.trim().length() > 0 && !ProfilePicPath.isEmpty()) {
            File image_file = new File(ProfilePicPath);
            if (image_file.exists()) {
                Bitmap newProfilePic = BitmapFactory.decodeFile(ProfilePicPath);
                // Code to manage the bitmap
            }
        }
    }
}






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

Thanks For Reading, Wish you a Happy Coding....

October 24, 2013

Importing Image From Camera

            In this tutorial I want to explain how to take a picture in camera ans import that picture to an ImageView using Intent,

In this I am having One Button and One ImageView. While Tap on the button it will open Camera. After that the user can take photo After taking photo it will ask for save image. If the user save the image then it will display on the Imageview.

You can also Inport Image from Camera with the help if File Provider. I am Explains about Capture Image using Camera with the Help of File provider From Here


activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout 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"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert Image" />

    <ImageView
        android:id="@+id/ImgPhoto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

MainActivity.java


import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
   
    private static final int CAMERA_PIC_REQUEST = 22;

    Uri cameraUri;
   
    Button BtnSelectImage;
    private ImageView ImgPhoto;
    private String Camerapath ;
   
   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        ImgPhoto = (ImageView) findViewById(R.id.ImgPhoto);
       
        BtnSelectImage = (Button) findViewById(R.id.BtnSelectImg);
        BtnSelectImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                try {
                       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                       startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();
                }
            }
        });
       
    }
   
   
    @Override
    public void onActivityResult(final int requestCode, int resultCode, Intent data) {
        try {
            switch (requestCode) {
           case CAMERA_PIC_REQUEST:
                if (resultCode == RESULT_OK) {
                    try {
                          Bitmap photo = (Bitmap) data.getExtras().get("data");
                         
                          ImgPhoto.setImageBitmap(photo);   
                         
                    } catch (Exception e) {
                        Toast.makeText(this, "Couldn't load photo", Toast.LENGTH_LONG).show();
                    }
                }
                break;
              default:
                break;
            }
        } catch (Exception e) {
        }
    }
    
   
}