Showing posts with label Capturing. Show all posts
Showing posts with label Capturing. Show all posts

Tuesday, 28 February 2017

Select Image From Gallery Android

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_weight="1"
    android:orientation="vertical"
    tools:context="com.androidsimplifycodes.example.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:gravity="center">

        <Button
            android:id="@+id/btnGallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gallery"
            android:textColor="@android:color/black"
            android:textStyle="bold" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8"
        android:gravity="center"
        android:padding="15dp">

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@null"
            android:scaleType="fitXY"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/boarder" />

    </RelativeLayout>

</LinearLayout>
boarder.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <stroke
        android:width="1.5dip"
        android:color="#000000" />
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
</shape>
MainActivity.java

package com.androidsimplifycodes.example;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    protected Button btnGallery;
    protected ImageView ivImage;

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

        btnGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                // start activity for result with request code
                startActivityForResult(i, 17);
            }
        });
    }

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

        if (resultCode == Activity.RESULT_OK && requestCode == 17) {
            // get the data from the getData() method which is return the Uri of the selected image
            Uri uri = data.getData();
            String[] filePath = {MediaStore.Images.Media.DATA};
            // fetch the selected image path using content resolver
            // which is return the object of the cursor
            Cursor cursor = getContentResolver().query(uri, filePath, null, null, null);
            // this one mandatory for the developer to check cursor is null or not
            if (cursor != null) {
                // set the cursor at the first position or move cursor pointer at the beginning
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePath[0]);
                String picturePath = cursor.getString(columnIndex);
                // this one also mandatory for the developer to close the open cursor
                cursor.close();

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                // required bitmap into ARGB_8888 format
                Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);
                ivImage.setImageBitmap(bitmap);
            }
        }
    }

    private void initView() {
        btnGallery = (Button) findViewById(R.id.btnGallery);
        ivImage = (ImageView) findViewById(R.id.ivImage);
    }
}
Output :


Monday, 27 February 2017

Android Capture Image Using Camera

    Add this three permission into your manifest file because we can stored captured image into app cache. but if you use android marshmallow then required run time permission.
AndroidManifest.xml

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

    <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" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name="com.androidsimplifycodes.example.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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    tools:context="com.androidsimplifycodes.example.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:gravity="center">

        <Button
            android:id="@+id/btnCamera"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Camera"
            android:textColor="@android:color/black"
            android:textStyle="bold" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8"
        android:gravity="center"
        android:padding="15dp">

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@null"
            android:scaleType="fitXY"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/boarder" />

    </RelativeLayout>

</LinearLayout>
boarder.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <stroke
        android:width="1.5dip"
        android:color="#000000" />
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
</shape>
MainActivity.java

package com.androidsimplifycodes.example;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    protected Button btnCamera;
    protected ImageView ivImage;
    protected File tempFile = null;
    protected String imgPath;
    protected Uri uri;

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

        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    // create temp file into cache with .png extension.
                    tempFile = File.createTempFile("cameraImg", ".png", getExternalCacheDir());
                    // get temp file full path
                    imgPath = tempFile.getAbsolutePath();
                    // your file path should be required to converted into Uri format.
                    uri = Uri.fromFile(tempFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                // image capture event set into Intent
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                // captured image stored at given uri
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                // start activity for result with intent
                // and request code is 13(set according to your requirement)
                startActivityForResult(intent, 13);
            }
        });
    }

    private void initView() {
        btnCamera = (Button) findViewById(R.id.btnCamera);
        ivImage = (ImageView) findViewById(R.id.ivImage);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 13) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            // get captured image from the cache with preferred configuration is ARGB_8888
            Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
            ivImage.setImageBitmap(bitmap);
        }
    }
}
Output :