Latest News

Take Picture With Camera - Android Studio - Java

In this Video we will capture image with Camera using the Camera Intent. Camera will be opened after clicking the button. If image is captured it will be set to an ImageView.
We will require following permission: 
1) CAMERA 
2) WRITE_EXTERNAL STORAGE 
So we will also handle Runtime Permission for OS Marshmallow and above.
>>Watch For Kotlin

VIDEO



Step 2: Code

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.blogspot.atifsoftwares.takepicture_java"><!--Adding Camera , Write External Storage Permission--><uses-permission android:name="android.permission.CAMERA"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><applicationandroid: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>

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"android:orientation="vertical"android:gravity="center_horizontal"tools:context=".MainActivity"><!--Image view in which image will be set--><ImageViewandroid:id="@+id/image_view"android:scaleType="centerCrop"android:src="@drawable/ic_image_black_24dp"android:layout_width="400dp"android:layout_height="400dp" /><!--Button to capture image--><Buttonandroid:id="@+id/capture_image_btn"android:text="Capture Image"style="@style/Base.Widget.AppCompat.Button.Colored"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>

MainActivity.java
package com.blogspot.atifsoftwares.takepicture_java;import android.Manifest;import android.content.ContentValues;import android.content.Intent;import android.content.pm.PackageManager;import android.net.Uri;import android.os.Build;import android.provider.MediaStore;import android.support.annotation.NonNull;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 {private static final int PERMISSION_CODE = 1000;private static final int IMAGE_CAPTURE_CODE = 1001;Button mCaptureBtn;ImageView mImageView;Uri image_uri;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mImageView = findViewById(R.id.image_view);mCaptureBtn = findViewById(R.id.capture_image_btn);//button clickmCaptureBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//if system os is >= marshmallow , request runtime permissionif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){if (checkSelfPermission(Manifest.permission.CAMERA) ==PackageManager.PERMISSION_DENIED ||checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==PackageManager.PERMISSION_DENIED){//permission not enabled , request itString[] permission = {Manifest.permission.CAMERA , Manifest.permission.WRITE_EXTERNAL_STORAGE};//show popup to request permissionsrequestPermissions(permission , PERMISSION_CODE);}else {//permission already grantedopenCamera();}}else {//system os < marshmallowopenCamera();}}});}private void openCamera() {ContentValues values = new ContentValues();values.put(MediaStore.Images.Media.TITLE , "New Picture");values.put(MediaStore.Images.Media.DESCRIPTION , "From the Camera");image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , values);//Camera intentIntent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT , image_uri);startActivityForResult(cameraIntent , IMAGE_CAPTURE_CODE);}//handling permission result@Overridepublic void onRequestPermissionsResult(int requestCode , @NonNull String[] permissions , @NonNull int[] grantResults) {//this method is called , when user presses Allow or Deny from Permission Request Popupswitch (requestCode){case PERMISSION_CODE:{if (grantResults.length > 0 && grantResults[0] ==PackageManager.PERMISSION_GRANTED){//permission from popup was grantedopenCamera();}else {//permission from popup was deniedToast.makeText(this , "Permission denied..." , Toast.LENGTH_SHORT).show();}}}}@Overrideprotected void onActivityResult(int requestCode , int resultCode , Intent data) {//called when image was captured from cameraif (resultCode == RESULT_OK){//set the image captured to our ImageViewmImageView.setImageURI(image_uri);}}}

Step 3: Run Project

Output
Take Picture with Camera - Android Studio - Java

0 Response to "Take Picture With Camera - Android Studio - Java"