How to Add bitmaps from Camera Activity Dynamically to Gallery
So I spent a few hours this morrning looking for a demo on how to add bitmaps that I got from the Camera Activity Dynamicly to a Gallery View. Here is the code I came up with. Let me know if there is a better way.
import java.util.ArrayList;
import java.util.Stack;
import java.util.Vector;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
public class Pictures extends Activity implements OnClickListener {
private static final int PICTURE_RESULT = 1;
Context ctx = null;
Button next = null;
Button addPhoto = null;
Gallery gal = null;
ImageAdapter images =null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pictures);
ctx = this.getBaseContext();
next = (Button) this.findViewById(R.id.widget30);
next.setOnClickListener(this);
addPhoto = (Button) this.findViewById(R.id.widget35);
addPhoto.setOnClickListener(this);
images = new ImageAdapter(this);
gal = (Gallery) this.findViewById(R.id.widget36);
gal.setAdapter(images);
}
@Override
public void onClick(View v) {
if (v == next) {
return;
} else if (v == addPhoto) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.startActivityForResult(intent, PICTURE_RESULT);
}
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (reqCode == PICTURE_RESULT) {
if (resultCode == Activity.RESULT_OK) {
Bundle b = data.getExtras();
Bitmap pic = (Bitmap) b.get("data");
if (pic != null) {
images.AddImage(pic);
gal.setAdapter(images); //forces the gallery to add the new images.
}
}
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Vector<Bitmap> images = new Vector<Bitmap>();
public ImageAdapter(Context c) {
mContext = c;
}
public void AddImage(Bitmap b)
{
images.add(b);
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object getItem(int arg0) {
return images.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img;
if(convertView ==null)
{
img = new ImageView(mContext);
}
else
{
img = (ImageView)convertView;
}
img.setImageBitmap(images.get(position));
return img;
}
}
}
import java.util.Stack;
import java.util.Vector;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
public class Pictures extends Activity implements OnClickListener {
private static final int PICTURE_RESULT = 1;
Context ctx = null;
Button next = null;
Button addPhoto = null;
Gallery gal = null;
ImageAdapter images =null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pictures);
ctx = this.getBaseContext();
next = (Button) this.findViewById(R.id.widget30);
next.setOnClickListener(this);
addPhoto = (Button) this.findViewById(R.id.widget35);
addPhoto.setOnClickListener(this);
images = new ImageAdapter(this);
gal = (Gallery) this.findViewById(R.id.widget36);
gal.setAdapter(images);
}
@Override
public void onClick(View v) {
if (v == next) {
return;
} else if (v == addPhoto) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.startActivityForResult(intent, PICTURE_RESULT);
}
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (reqCode == PICTURE_RESULT) {
if (resultCode == Activity.RESULT_OK) {
Bundle b = data.getExtras();
Bitmap pic = (Bitmap) b.get("data");
if (pic != null) {
images.AddImage(pic);
gal.setAdapter(images); //forces the gallery to add the new images.
}
}
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Vector<Bitmap> images = new Vector<Bitmap>();
public ImageAdapter(Context c) {
mContext = c;
}
public void AddImage(Bitmap b)
{
images.add(b);
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object getItem(int arg0) {
return images.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img;
if(convertView ==null)
{
img = new ImageView(mContext);
}
else
{
img = (ImageView)convertView;
}
img.setImageBitmap(images.get(position));
return img;
}
}
}
And the xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget34"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<Button
android:id="@+id/widget35"
android:layout_width="209px"
android:layout_height="wrap_content"
android:text="Add Photo"
>
</Button>
<Gallery
android:id="@+id/widget36"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</Gallery>
<Button
android:id="@+id/widget30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
>
</Button>
</LinearLayout>
<LinearLayout
android:id="@+id/widget34"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<Button
android:id="@+id/widget35"
android:layout_width="209px"
android:layout_height="wrap_content"
android:text="Add Photo"
>
</Button>
<Gallery
android:id="@+id/widget36"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</Gallery>
<Button
android:id="@+id/widget30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
>
</Button>
</LinearLayout>
I’m new to android coding and the only thing that keeps me from testing this is Getters.class? Is this a class that you have created that I’m missing… I’ve been waiting for this code in leiu of other code that I have written or found.
Sorry about that, its from the project I’m working on. You can just remove the line:
startActivity(new Intent(ctx, Getters.class));
I will update the article.
Thanks very helpful code, worked very good for me