Open top menu

Show Capture Image in GridView Dynamically in Android?

In this blog we will implement how to take picture via Camera and how to show capture image in GridView (Widget). Explain Full Activity…

Selecteditem.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:orientation="vertical">





    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text=" Camra "

        android:id="@+id/button"

        android:drawableBottom="@drawable/camra"

        android:layout_margin="20dp"

        />

    <GridView

        android:layout_width="wrap_content"

        android:layout_height="match_parent"

        android:id="@+id/gridView"

        android:numColumns="6"

        android:layout_margin="6dp"/>





</LinearLayout>
……………………………………………………………………………………………………

Image_cover.xml.
……………………………………………………………………………………………………..
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle" android:padding="15dp">

    <solid android:color="#ffffff"/>



    <corners

        android:bottomRightRadius="0dp"

        android:bottomLeftRadius="0dp"

        android:topLeftRadius="0dp"

        android:topRightRadius="0dp"/>



    <stroke android:width="2dip" android:color="#1084ba" />

</shape>
…………………………………………………………………………………………………………………..
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:orientation="vertical">





    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text=" Camera "

        android:id="@+id/button"

        android:drawableBottom="@drawable/camra"

        android:layout_margin="20dp"

        />

    <GridView

        android:layout_width="wrap_content"

        android:layout_height="match_parent"

        android:id="@+id/gridView"

        android:numColumns="6"

        android:layout_margin="6dp"/>





</LinearLayout>
………………………………………………………………………………………………….
ColorItem.java
………………………………………………………………………………………………
public class ColorItem {

    private String ColorName;

    private Bitmap bitmap;



    public Bitmap getBitmap() {

        return bitmap;

    }



    public void setBitmap(Bitmap bitmap) {

        this.bitmap = bitmap;

    }



    public String getColorName() {

        return ColorName;

    }



    public void setColorName(String colorName) {

        ColorName = colorName;

    }

}

………………………………………………………………………………………………

 
SelectedImageAdapter.java
……………………………………………………………………………………………….
 



import android.content.Context;

import android.graphics.Color;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;



import java.util.ArrayList;

import java.util.Locale;


 public class SelectedImageAdapter extends BaseAdapter {

    private Context context;



    ArrayList myList = new ArrayList();

    private ArrayList<ColorItem> arraylist;

    int resorce;



    public SelectedImageAdapter(Context context, ArrayList<ColorItem> myList) {

        this.context = context;

        this.myList = myList;

        this.arraylist = new ArrayList<ColorItem>();

        this.arraylist.addAll(myList);



    }



    @Override

    public int getCount() {

        return myList.size();

    }



    @Override

    public ColorItem getItem(int position) {



        return (ColorItem) myList.get(position);

    }



    @Override

    public long getItemId(int position) {

        return 0;

    }



    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        ColorItem speciitem = getItem(position);

        final ViewHolder holder;

        if (convertView == null) {

            LayoutInflater infalInflater = (LayoutInflater) context

                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);

            convertView = infalInflater.inflate(R.layout.selected_item, null);

        }

        holder = new ViewHolder();

        try {



            holder.colors = (ImageView) convertView.findViewById(R.id.colors);

            holder.colorsname = (TextView)    
convertView.findViewById(R.id.color_text);

            holder.colors.setImageBitmap(speciitem.getBitmap());

            holder.colorsname.setText(speciitem.getColorName());





        } catch (Exception e) {

            e.printStackTrace();

        }

        convertView.setTag(holder);

        return convertView;

    }



    static class ViewHolder {



        TextView colorsname, bagcount;

        ImageView colors;

    }



}

……………………………………………………………………………………………..

MainActivity.java.
……………………………………………………………………………………………….
 

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Environment;

import android.provider.MediaStore;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.GridView;

import android.widget.Toast;



import java.io.File;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.Locale;



public class MainActivity extends AppCompatActivity {

    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;

    public static final int MEDIA_TYPE_IMAGE = 1;

    GridView gridView;

    // directory name to store captured images and videos

    private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";

    Bitmap decodedByte;

    private Uri fileUri;

    int count=0;

    ArrayList<ColorItem>   CamraTakeImgList = new ArrayList<>();

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button takepic=(Button)findViewById(R.id.button);

        takepic.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                captureImage();

            }

        });

        gridView=(GridView)findViewById(R.id.gridView);

    }

    private void captureImage() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);



    }

    public Uri getOutputMediaFileUri(int type) {

        return Uri.fromFile(getOutputMediaFile(type));

    }



    private static File getOutputMediaFile(int type) {



        // External sdcard location

        File mediaStorageDir = new File(

                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),

                IMAGE_DIRECTORY_NAME);



        // Create the storage directory if it does not exist

        if (!mediaStorageDir.exists()) {

            if (!mediaStorageDir.mkdirs()) {

                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "

                        + IMAGE_DIRECTORY_NAME + " directory");

                return null;

            }

        }



        // Create a media file name

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",

                Locale.getDefault()).format(new Date());

        File mediaFile;

        if (type == MEDIA_TYPE_IMAGE) {

            mediaFile = new File(mediaStorageDir.getPath() + File.separator

                    + "IMG_" + timeStamp + ".jpg");

        } else {

            return null;

        }

        return mediaFile;

    }



    private void previewCapturedImage() {

        try {

            count++;

            BitmapFactory.Options options = new BitmapFactory.Options();

            options.inSampleSize = 8;

            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);

            ColorItem select = new ColorItem();

            select.setBitmap(bitmap);

            select.setColorName("Pic"+" "+count);

            CamraTakeImgList.add(select);

            SelectedImageAdapter seletimg = new SelectedImageAdapter(MainActivity.this, CamraTakeImgList);

            gridView.setAdapter(seletimg);

        } catch (NullPointerException e) {

            e.printStackTrace();

        }

    }

    // Find Device Bluetooth is Enable or Not.......................................................

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

        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {

            if (resultCode == RESULT_OK) {

                previewCapturedImage();

            }

        }

    }

}
 

…………………………………………………………………………………………….......................


0 comments