Saturday, April 9, 2016

Take Photo & Saved Image in sdcard with Intent Part-I

"Jai Saraswati Maa"

Hi ... Dear All ..Today I am uploading a superb example of Taking photo 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 image view  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"

    android:layout_width="match_parent" android:layout_height="match_parent">


    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Take Photo"        android:id="@+id/button2"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true" />

    <ImageView        android:layout_width="300dp"

        android:layout_height="300dp"        android:id="@+id/imageView"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true" />
</RelativeLayout>

B. Java Code 

package com.exam.ravi.cameraex1;

import android.content.Intent;
import android.graphics.drawable.Drawable;
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;

public class SecondAct extends AppCompatActivity {
    Button button;
    ImageView imageView;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sec_act);
        button = (Button) findViewById(R.id.button2);
        imageView = (ImageView) findViewById(R.id.imageView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File filePath = getFile();
                camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(filePath));
                startActivityForResult(camera,1);

            }
        });
    }
    private File getFile()
    {
        File folder = new File("sdcard/my_images");
        if(!folder.exists())
        {
            folder.mkdir();
        }
        File imgpath = new File(folder,"me.jpg");
                  
        return imgpath;
    }

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String path =  "sdcard/my_images/me.jpg";
        imageView.setImageDrawable(Drawable.createFromPath(path));
    }
}


No comments:

Post a Comment