Saturday, April 9, 2016

Take Photo & Saved Multiple Images in sdcard with Intent Part-II

"Jai Saraswati Maa"

Hi ... Dear All ..Today I am uploading a superb example of Taking Multiple photos and saved in sdcard  using camera App .
Image will be saved at your specified path.

Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
***** check on physical device
Suggestion - Add Features and permissions in manifest file like  

<uses-feature android:name="android.hardware.camera2" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


A. Add a Button and add a method on onClick  in layout file like 
   our xml code is 

<?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: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="com.exam.ravi.cameraex1.MainActivity">

    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Click Photo"        android:id="@+id/button"

        android:onClick="photo"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="102dp" />
</RelativeLayout>

B. Java File  

package com.exam.ravi.cameraex1;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    File imagePath;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void photo(View view)
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                              // Get File Path 
        String TEMP_DIR = Environment.getExternalStorageDirectory() +
              File.separator + Calendar.getInstance().getTime() +".jpg"
                  imagePath =  new File(TEMP_DIR);
         Uri imgUri = Uri.fromFile(imagePath);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,imgUri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
        startActivityForResult(intent,1);
    }

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==1)
        {
            switch (resultCode)
            {
                case Activity.RESULT_OK:
                    if(imagePath.exists())
                    {
                        Toast.makeText(this,"File saved path is "+
                        imagePath.getAbsolutePath(),Toast.LENGTH_LONG).show();
                    }
                    else                    {
                 Toast.makeText(this,"File Not saved " ,Toast.LENGTH_LONG).show();

                    }
                case Activity.RESULT_CANCELED:
                    break;
            }
        }
    }
}

No comments:

Post a Comment