I am working on android application in which i want to save and retrieve images from SQL-Lite DB using blob. My condition is that, it first download the image from url and then save image file and image url in db, for second time if the URL is already present in the DB then it will give me that image file, if not then it save the URL with ImageFile. I am using the following method to download image from URL, but didn't get the above condition.
public class MainActivity extends Activity implements OnClickListener{
private ImageView imageview=null;
private Button btninsert=null;
private Button btnretrive=null;
private MyDataBase mdb=null;
private SQLiteDatabase db=null;
private Cursor c=null;
private byte[] img=null;
private static final String DATABASE_NAME = "ImageDb.db";
public static final int DATABASE_VERSION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btninsert=(Button)findViewById(R.id.button_insert);
btnretrive= (Button)findViewById(R.id.button_retriev);
imageview= (ImageView)findViewById(R.id.imageView_image);
imageview.setImageResource(0);
btninsert.setOnClickListener(this);
btnretrive.setOnClickListener(this);
mdb=new MyDataBase(getApplicationContext(), DATABASE_NAME,null, DATABASE_VERSION);
String SenderImageURL= "http://ift.tt/1Gxh8lj";
new DownloadImageTask((ImageView) findViewById(R.id.imageView_image)).execute(SenderImageURL);
imageview.setVisibility(View.VISIBLE);
Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
img=bos.toByteArray();
db=mdb.getWritableDatabase();
}
@Override
public void onClick(View arg0) {
if(btninsert==arg0)
{
ContentValues cv=new ContentValues();
cv.put("image", img);
db.insert("tableimage", null, cv);
Toast.makeText(this, "inserted successfully", Toast.LENGTH_SHORT).show();
}
else if(btnretrive==arg0)
{
String[] col={"image"};
c=db.query("tableimage", col, null, null, null, null, null);
if(c!=null){
c.moveToFirst();
do{
img=c.getBlob(c.getColumnIndex("image"));
}while(c.moveToNext());
}
Bitmap b1=BitmapFactory.decodeByteArray(img, 0, img.length);
imageview.setImageBitmap(b1);
imageview.setVisibility(View.VISIBLE);
Toast.makeText(this, "Retrive successfully", Toast.LENGTH_SHORT).show();
}
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class MyDataBase extends SQLiteOpenHelper{
public MyDataBase(Context context, String dbname, CursorFactory factory, int dbversion) {
super(context, dbname, factory, dbversion);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table tableimage(image blob);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Aucun commentaire:
Enregistrer un commentaire