dimanche 29 mars 2015

Retriving image data from SQLite database

I am writing a code to insert an image from gallery into SQLite and display it in another activity. I am able to insert the image into the database but I am not able to retrieve it. Can you please help me? Thanks in advance This is the code to insert image into database



public class Register extends Activity {
ImageView img;
String picpath;
Bitmap bmp;
Canvas cnvs;
Random r;
int x,y,height,width;
ByteArrayOutputStream blob;
//SQL sql;
Paint paint;
String name,no;
Uri data;
SQLiteDatabase db;
String[] filePath={MediaStore.Images.Media.DATA};;
Cursor cur;
TextView txt;
int colIndex;
ContentValues ctv;
byte[] imgData;


public static final String DB_NAME="New.db";
public static final String TABLE_NAME="Sample";
public static final int DB_VERSION=1;

public static final String NAME="Name";
public static final String NO="No";
public static final String IMG="Img";
public static final String PIXELX="PosX";
public static final String PIXELY="PosY";

public void create()
{
db=openOrCreateDatabase(DB_NAME,Context.MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"("+IMG+" BLOB );");
db.close();
}

public void insert()
{
create();
db=openOrCreateDatabase(DB_NAME,Context.MODE_WORLD_WRITEABLE,null);
db.execSQL("DELETE FROM "+TABLE_NAME);
String s=db.getPath();
// ctv.put(NAME,"Vignesh");
//ctv.put(NO,"9578323986");
try
{
FileInputStream img_file=new FileInputStream(picpath);
BufferedInputStream bis=new BufferedInputStream(img_file);
imgData=new byte[bis.available()];
bis.read();
ctv.put(IMG, imgData);
long val=db.insert(TABLE_NAME,null, ctv);
if(val<0){
Toast.makeText(this.getBaseContext(),"Image not Saved",Toast.LENGTH_SHORT).show();
}
else
{
txt.append(s);
txt.append("\n Name "+imgData.length);

Toast.makeText(this.getBaseContext(),"Image Saved",Toast.LENGTH_SHORT).show();
}
bis.close();
}
catch(Exception e)
{
e.getMessage();
}

//ctv.put(PIXELX, x);
//ctv.put(PIXELY,y);
db.close();
}


protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);


ctv=new ContentValues();
img=(ImageView)findViewById(R.id.imageView1);
//sql=new SQL(this);
//db=sql.getWritableDatabase();
//sql.drop(db);
//sql.onCreate(db);
r=new Random();
paint=new Paint();
x=r.nextInt(100);
blob=new ByteArrayOutputStream();
//create();

Intent i=getIntent();
name=i.getStringExtra("name");
no=i.getStringExtra("no");
//Open Button
Button open=(Button)findViewById(R.id.button3);
open.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent gal_open=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gal_open,1);

}
});
txt=(TextView)findViewById(R.id.textView1);

Button shuffle=(Button)findViewById(R.id.button2);
shuffle.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

bmp=Bitmap.createBitmap(img.getHeight(),img.getWidth(),Bitmap.Config.RGB_565);
//bmp=BitmapFactory.decodeFile(picpath);
cnvs=new Canvas(bmp);

height=img.getMeasuredHeight();
width=img.getMeasuredWidth();
x=r.nextInt(height);
y=r.nextInt(width);
paint.setColor(Color.RED);


txt.setText(x+","+y);
cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);
cnvs.drawRect(x, y,x+20,y+20 , paint);
img.setImageBitmap(bmp);

}
});


Button ok= (Button)findViewById(R.id.button1);
txt=(TextView)findViewById(R.id.textView1);

ok.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
create();
insert();

//startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
});
}


public void onActivityResult(int requestCode,int resultCode,Intent intentData)
{
super.onActivityResult(requestCode, resultCode, intentData);
if(requestCode==1 && resultCode==RESULT_OK && intentData!=null)
{

bmp=Bitmap.createBitmap(img.getHeight(),img.getWidth(),Bitmap.Config.RGB_565);
cnvs=new Canvas(bmp);
//img.setImageBitmap(bmp);

height=img.getMeasuredHeight();
width=img.getMeasuredWidth();
x=r.nextInt(height);
y=r.nextInt(width);
paint.setColor(Color.RED);

data=intentData.getData();

cur=getContentResolver().query(data,filePath,null,null,null );
cur.moveToFirst();

colIndex=cur.getColumnIndex(filePath[0]);
picpath=cur.getString(colIndex);
cur.close();

txt=(TextView)findViewById(R.id.textView1);
txt.setText(x+","+y);
cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);
cnvs.drawRect(x, y,x+20,y+20 , paint);
img.setImageBitmap(bmp);
}
}


}


This is the code to retrieve the image



public class Login extends Activity {
//SQL sql;
User user;
String name;
TextView no;
ImageView img;
int imgx,imgy;
View touchlistener;
SQLiteDatabase db;
byte[] imgData1;
Cursor cur;
int pixelx,pixely;
public static final String DB_NAME="New.db";
public static final String TABLE_NAME="Sample";
public static final int DB_VERSION=1;

public static final String NAME="Name";
public static final String NO="No";
public static final String IMG="Img";
public static final String PIXELX="PosX";
public static final String PIXELY="PosY";

@SuppressLint("NewApi") public void retrieve()
{

db=openOrCreateDatabase(DB_NAME,Context.MODE_PRIVATE,null);


cur=db.query(true,TABLE_NAME,null,null,null,null, null,null, null,null);
cur.moveToFirst();

imgData1=cur.getBlob(cur.getColumnIndex("Img"));

img.setImageBitmap(BitmapFactory.decodeByteArray(imgData1, 0,imgData1.length));

cur.close();
db.close();


}



public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.login);


img=(ImageView)findViewById(R.id.imageView1);



retrieve();
}

}

Aucun commentaire:

Enregistrer un commentaire