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>