import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
public static final String id = "id";
public static final String data = "image";
public static final String databasename="dbex";
public static final String tablename="login";
private static final int dbversion=1;
private static final String create="create table login(id integer primary key autoincrement,image blob);";
public static final String[] PROJECTION_ALL = new String[] {
id,data};
private SQLiteDatabase db;
private DbHelper helper;
private final Context context;
//byte[] image;
public DBAdapter(Context con)
{
this.context=con;
helper=new DbHelper(context);
}
public class DbHelper extends SQLiteOpenHelper
{
public DbHelper(Context context) {
super(context, databasename, null, dbversion);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(create);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w("Login DB","upgrading database from"+oldVersion+"to"+newVersion+"which destroy all data");
if(oldVersion==1)
{
db.execSQL("DROP TABLE IF EXIST LOGIN ");
}
onCreate(db);
}
}
public DBAdapter open()throws SQLException
{
db=helper.getWritableDatabase();
return this;
}
public void close()
{
helper.close();
}
public long insert(byte[] image1)
{
ContentValues cv=new ContentValues();
cv.put(data, image1);
return db.insert(tablename, null, cv);
}
public Cursor fetchAll()
{
Cursor c=db.rawQuery("SELECT image FROM LOGIN" , null);
return c;
/*Cursor c=db.query(tablename, PROJECTION_ALL,null, null,null,null,null);
byte[] image=null;
if(c.getCount()>0)
{
c.moveToNext();
image=c.getBlob(c.getColumnIndex(data));
return image;
}
else
{
return null;
}*/
}
public byte[] fetchSingle(int id1)
{
Cursor c=db.query(tablename, PROJECTION_ALL, id+"="+id1, null,null,null,null);
if(c==null)
{
return null;
}
byte[] image=null;
if(c.moveToFirst())
{
image=c.getBlob(c.getColumnIndexOrThrow(data));
}
c.close();
return image;
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Dbimage extends Activity {
/** Called when the activity is first created. */
Button b1;
DBAdapter db;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),Show.class);
startActivity(i);
}
});
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class Show extends Activity{
//Integer i[]={R.drawable.cute,R.drawable.icon,R.drawable.dolphin};
ArrayList<byte[]> a;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
DBAdapter db=new DBAdapter(this);
//ImageView im=(ImageView)findViewById(R.id.imageView1);
///
db.open();
byte[] img=Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.dolphin));
byte[] img1=Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.cute));
db.insert(img);
db.insert(img1);
/*
byte[] b=db.fetchSingle(1);
im.setImageBitmap(Utilities.getImage(b));*/
Cursor c=db.fetchAll();
System.out.println("The Lenght of the Cursor:::::::::::::::::::::::::::::::::"+c.getCount());
a=new ArrayList<byte[]>(c.getCount());
c.moveToFirst();
while(!c.isAfterLast())
{
final byte[] b=db.fetchSingle(c.getPosition()+1);
//ByteArrayInputStream input=new ByteArrayInputStream(b);
//Bitmap map=BitmapFactory.decodeStream(input);
//c.getBlob(c.getColumnIndex("data"));
a.add(b);
GridView gal=(GridView)findViewById(R.id.gridView1);
gal.setAdapter(new ShowAdapter(this, a));
c.moveToNext();
gal.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),Display1.class);
i.putExtra("image", b);
Bundle b=new Bundle();
b.putInt("position",arg2);
i.putExtras(b);
startActivity(i);
}
});
}
// db.insert(img1);
//byte[] b=db.fetchAll();
//im.setImageBitmap(Utilities.getImage(img1));
//im.setImageBitmap(Utilities.getImage(img));
db.close();
//gal.setAdapter(new ShowAdapter(this));
//db=new DBAdapter(this);
// byte[] b=db.fetchSingle(1);
//img1.setImageBitmap(Utilities.getImage(img));
}
class ShowAdapter extends BaseAdapter{
Context c;
ArrayList<byte[]> a;
ShowAdapter(Context con,ArrayList<byte[]> a){
c=con;
this.a=a;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return a.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//LinearLayout ll=new LinearLayout(c);
/*GridView gal=new GridView(c);
gal.setBackgroundResource(R.drawable.cute);
ll.addView(gal);*/
ImageView img;
if(convertView==null)
{
img=new ImageView(c);
img.setLayoutParams(new GridView.LayoutParams(50,50));
img.setPadding(3, 3, 3, 3);
}
else
{
img=(ImageView)convertView;
}
img.setImageBitmap(Utilities.getImage(a.get(position)));
return img;
//return ll;
}
}
}
Utility.java
import java.io.ByteArrayOutputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
public class Utilities {
public static byte[] getBytes(Bitmap bitmap)
{
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG,0, stream);
return stream.toByteArray();
}
public static Bitmap getImage(byte[] image)
{
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
Display.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class Display1 extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.show1);
Bundle b1=new Bundle();
int i=b1.getInt("position");
byte b[]=getIntent().getByteArrayExtra("image");
ImageView im=(ImageView)findViewById(R.id.imageView1);
im.setImageBitmap(Utilities.getImage(b));
}
}