android - How to store or save and retrieve multiple remote images locally -
android - How to store or save and retrieve multiple remote images locally -
i looking solution in android store remote images (urls retrieved webservices) local (sdcard or sqlite db or cache) images can displayed in offline-mode too. because want display each time image during call.
is there solution that?
please follow these steps (for 1 image retrieval @ time):
retrieve image bitmap url. convert bitmap byte array. store byte array blob field of sqlite db table. get blob sqlite db table. convert blob byte array. convert byte array bitmap. finally, set bitmap image onto imagemap of activity.public class imagestoreandretrievalactivity extends activity {
private imageview imageview; private databasehandler dbhandler; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_image_store); imageview = (imageview)findviewbyid(r.id.imageview1); dbhandler = new databasehandler(getapplicationcontext()); new mythread("http://www.fnordware.com/superpng/pnggrad8rgb.png"); imageview.setimagebitmap(dbhandler.getbitmap(1)); // here 1 id image tbl_blob table in sqlite db. } public class mythread implements runnable { private string url; private bitmap bitmap = null; private thread th; public mythread(string url) { this.url = url; th = new thread(this); th.start(); seek { th.join(); } grab (interruptedexception e) { // todo auto-generated grab block e.printstacktrace(); } } public void run() { url fileurl = null; seek { fileurl = new url(url); httpurlconnection conn = (httpurlconnection) fileurl.openconnection(); conn.setconnecttimeout(0); conn.setdoinput(true); conn.connect(); inputstream = conn.getinputstream(); bitmap = bitmapfactory.decodestream(is); is.close(); } grab (ioexception e) { e.printstacktrace(); } dbhandler.insertbitmap(bitmap); log.i("url", url); } }
}
public static final string database_name = "blobdb"; public static final int database_version = 1; public static final string create_blob_table = "create table if not exists tbl_blob " + "(id integer primary key autoincrement, img blob not null, description text null)";
public class databasehandler extends sqliteopenhelper { private context context;
public databasehandler(context context) { super(context, database_name, null, database_version); this.context = context; } // creating tables @override public void oncreate(sqlitedatabase db) { // create tables in 'btlocdb' createtables(db); } /** create tables in 'btlocdb'. * @param db */ public void createtables(sqlitedatabase db) { db.execsql(create_blob_table); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // drop older table if existed //db.execsql("drop table if exists " + consts.table.database_name); // create tables 1 time again //oncreate(db); } public void insertbitmap(bitmap bm) { bytearrayoutputstream out = new bytearrayoutputstream(); bm.compress(bitmap.compressformat.png, 100, out); byte[] buffer=out.tobytearray(); sqlitedatabase db = this.getwritabledatabase(); // starts transaction. db.begintransaction(); contentvalues values; seek { values = new contentvalues(); values.put("img", buffer); values.put("description", "this description"); // inserting row long = db.insert("tbl_blob", null, values); log.i("insert", + ""); // insert or update info database successfully. db.settransactionsuccessful(); } grab (sqliteexception e) { e.printstacktrace(); } { db.endtransaction(); // ending transaction. db.close(); // closing database connection } } public bitmap getbitmap(int id){ bitmap bm = null; sqlitedatabase db = this.getwritabledatabase(); // starts transaction. db.begintransaction(); seek { string selectquery = "select * tbl_blob id = " + id; cursor cursor = db.rawquery(selectquery, null); log.i("cursor", cursor.getcount() + ""); if(cursor.getcount() > 0) { if (cursor.movetofirst()) { { byte[] blob = cursor.getblob(cursor.getcolumnindex("img")); bytearrayinputstream imagestream = new bytearrayinputstream(blob); bm = bitmapfactory.decodestream(imagestream); } while (cursor.movetonext()); } } db.settransactionsuccessful(); } grab (sqliteexception e) { e.printstacktrace(); } { db.endtransaction(); // ending transaction. db.close(); // closing database connection } homecoming bm; }
}
hope help you!!! allow me know if have query regarding issue. thanks..
android local-storage
Comments
Post a Comment