Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday 12 April 2015

Save the image in specific folder from the camera and delete after use

If you want save your capture image in specific folder(directory) then follow some instructions..

1-Create new Android Project

2-activity_main.xml

<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.samset.capturepic_savespecific_folder.MainActivity" >

    <Button
        android:id="@+id/btn_capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="90dp"
        android:text="Take Photo" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btn_capture"
        />


</RelativeLayout>

3-MainActivity.xml

package com.samset.capturepic_savespecific_folder;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity implements OnClickListener{
    Button btn_camera,btn_priview;
    ImageView show;
    static int CAMERA=200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_camera=(Button) findViewById(R.id.btn_capture);
show=(ImageView) findViewById(R.id.imageView1);
btn_camera.setOnClickListener(this);
}
   
 public void TakePic()
 {
 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                  // Create folder by name "MyImage" and save image by name "temp.png"
 File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
 imagesFolder.mkdirs();
 File image = new File(imagesFolder, "temp.png");
         // File f = new File(android.os.Environment.getExternalStorageDirectory(), "/My/temp.png");
          intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
          startActivityForResult(intent, CAMERA);
 
 }
 
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA) {
            // Get image from "MyImage" folder
                File f = new File(Environment.getExternalStorageDirectory().toString()+"/MyImages/");
                for (File temp : f.listFiles()) {
    //Check file in "MyImage" folder by name "temp.png"
                    if (temp.getName().equals("temp.png")) {
                        f = temp;
                        break;
                    }
                }
                try {

                   // Start code delete 
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
 
                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions); 
                   
                    show.setImageBitmap(bitmap);
                   // show.setVisibility(0);
                    String path = android.os.Environment.getExternalStorageDirectory()+ File.separator
                            + "phone" + File.separator + "default";
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".png");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
            }}
 }
 
@Override
public void onClick(View v) {
TakePic();
}
}

4- AndroidMainifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.samset.capturepic_savespecific_folder"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <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="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Resizing Image From camera and Gallery(CROPING)

First you create a new android project..


Follow below instruction....

1-Go "File" menu


2-"New"
3-"Android Application project"
4-And now Enter the "Application name","Project name", and "Packege name"

and follow instructions...


Now see..

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewAddCard"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Take Image"
        android:textSize="16dp"
        android:typeface="sans" />

    <Button
        android:id="@+id/btn_take_camera"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/textViewAddCard"
        android:text="Take From Camera"
        android:typeface="sans" />

    <Button
        android:id="@+id/btn_select_gallery"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/btn_take_camera"
        android:layout_marginTop="10dp"
        android:text="Select from Gallery"
        android:typeface="sans" />
<RelativeLayout
    android:layout_width="match_parent"
        android:layout_height="250dp"
    android:layout_below="@+id/btn_select_gallery"
   
    >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        />
   
</RelativeLayout>
</RelativeLayout>

 MainActivity.java


package com.samset.resizeimage_sample;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLERY = 2;
ImageView imgview;

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

imgview = (ImageView) findViewById(R.id.imageView1);
Button buttonCamera = (Button) findViewById(R.id.btn_take_camera);
Button buttonGallery = (Button) findViewById(R.id.btn_select_gallery);
buttonCamera.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

       // call  default camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());

      // crop image

intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 200);

try {

intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);

} catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
});
buttonGallery.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

 
Intent intent = new Intent();

      // call  gallery
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);

     //CROPING IMAGE
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 200);
intent.putExtra("aspectY", 150);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
intent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());

try {

intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_GALLERY);

} catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == PICK_FROM_CAMERA) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
imgview.setImageBitmap(photo);

}
}

if (requestCode == PICK_FROM_GALLERY) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap photo = extras2.getParcelable("data");
imgview.setImageBitmap(photo);

}
}
}

}

Mainifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.samset.resizeimage_sample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>