Blog purpose for android basic example for android app developer. any query please my contact

Friday, 10 April 2020

android studio camera app tutorial


In this Article Today learn android studio camera app tutorial. there are Intent camera Button Click open to the Camera Follow the Source code android studio camera app tutorial. for the more learn camera app create for Android Studio follow the developer google android

Camera is mainly used to capture pictures and videos. We can control the camera by using methods of camera api.

Android provides the facility to work on camera in 2 ways:

By Camera Intent
By Camera API


We Are Always Permission for AndroidManifest.xml:

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



follow the Source code android studio camera app tutorial

1.MainActivity.java:

package com.akash.cameraintent;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends Activity {
    private static final String TAG = "CameraLaunchingActivity";
    private final static int ACTION_TAKE_PICTURE = 123;

    private File pictureFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void takePicture(View v) {
        Log.d(TAG, "Starting Camera Activity");
        try {
            // Use an Intent to get the Camera app going.
            Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Set up file to save image into.
            File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            File pictureFile = new File(baseDir, "picture1234.jpg");
            imageIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            imageIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(pictureFile));
            // And away we go!
            startActivityForResult(imageIntent, ACTION_TAKE_PICTURE);
        } catch (Exception e) {
            Toast.makeText(this,
                    getString(R.string.cant_start_activity) + ": " + e,
                    Toast.LENGTH_LONG).show();
        }
    }

    /** Called when an Activity we started for Result is complete */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ACTION_TAKE_PICTURE:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        if (pictureFile.exists()) {
                            final String message = getString(R.string.picture_saved) + " " + pictureFile.getAbsoluteFile();
                            Log.d(TAG, message);
                            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
                        } else {
                            final String message = getString(R.string.picture_created_but_missing);
                            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
                        }
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();
                        break;
                    default:
                        Toast.makeText(this, "Unexpected resultCode: " + resultCode, Toast.LENGTH_LONG).show();
                        break;
                }
                break;
            default:
                Toast.makeText(this, "Unexpected requestCode: " + requestCode, Toast.LENGTH_LONG).show();
        }
    }
}




2.activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Camera!"
        android:id="@+id/textView"/>

    <Button
        android:text="Click Open Camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="23dp"
        android:layout_marginStart="23dp"
        android:layout_marginTop="81dp"
        android:id="@+id/button"
        tools:text="Take a picture!"
        android:onClick="takePicture"/>
</RelativeLayout>



3.strings.xml:

<resources>
    <string name="app_name">CameraIntent</string>
    <string name="cant_start_activity">Unable to start Camera activity!</string>
    <string name="picture_saved">Picture saved</string>
    <string name="picture_created_but_missing">Camera says it created image file but didn\'t give us correct location.</string>
</resources>



4.dimens.xml:

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>


5.AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.cameraintent">
<uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-feature android:name="android.hardware.camera"
        android:required="true" />
    <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>


No comments:

Post a Comment