Create an application to take picture using native application.


MainActivity.java


package com.example.assignment22;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
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 AppCompatActivity implements OnClickListener {

    Button btncapture;
    ImageView iv;

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

        btncapture=(Button) findViewById(R.id.btncapture);
        iv=(ImageView) findViewById(R.id.img);

        btncapture.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        try
        {
            Intent i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, 0);
        }catch(Exception e){
            Toast.makeText(this,e.toString(),1).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try
        {
            Bitmap mybitmap=(Bitmap) data.getExtras().get("data");
            //mybitmap.
            iv.setImageBitmap(mybitmap);
        }catch(Exception e){Toast.makeText(this,e.toString(),1).show();}
    }
}




activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <ImageView
        android:id="@+id/img"
        android:layout_width="635dp"
        android:layout_height="369dp"
        android:layout_marginStart="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginBottom="28dp"
        android:layout_weight="0.86"
        app:layout_constraintBottom_toTopOf="@+id/btncapture"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btncapture"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="172dp"
        android:text="Capture"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>




AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.assignment22">
    <uses-sdk android:minSdkVersion="8">

    </uses-sdk>
    <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>




Output


After taking picture



To download follow this link : Program 22

No comments:

Post a Comment