i have multiple asynctask's running in loop inside splash screen. want app stop execution until asynctasks complete.and update ui thread total number of async task , task's completed.like if there 3 tasks , 1 complete display 1/3 complete. code loop:-
string[] images = parse_imgjson.image; (int = 0; < images.length; i++) { log.d("image ", images[i]); download_img(images[i]); }
code download_img():-
public void download_img(string img_url) { string filename = img_url.substring(img_url.lastindexof('/') + 1, img_url.length()); file file = new file("/storage/emulated/0/rready_images/" + filename); if (file.exists() && !file.isdirectory()) { log.d("image exists", filename); } else { if (filename.contains(".jpg") || filename.contains(".gif") || filename.contains(".png")) { new downloadimagesasync().execute(img_url); } else { log.d("image download ", "failed " + filename); } } }
the actual async task code downloading file:-
class downloadimagesasync extends asynctask<string, string, string> { boolean issdpresent = android.os.environment.getexternalstoragestate().equals(android.os.environment.media_mounted); private string resp; int lengthoffile; @override protected string doinbackground(string... params) { int count; try { url url = new url(params[0]); urlconnection connection = url.openconnection(); connection.connect(); lengthoffile = connection.getcontentlength(); log.d("andro_async", "length of file : " + lengthoffile); string filename = params[0].substring(params[0].lastindexof('/') + 1, params[0].length()); log.d("filename", filename); resp = filename; if (issdpresent) { inputstream inputstream = new bufferedinputstream(url.openstream()); outputstream outputstream = new fileoutputstream("sdcard/rreadyreckoner_images/" + filename); byte data[] = new byte[1024]; long total = 0; while ((count = inputstream.read(data)) != -1) { total += count; outputstream.write(data, 0, count); } outputstream.flush(); outputstream.close(); inputstream.close(); } else { inputstream inputstream = new bufferedinputstream(url.openstream()); outputstream outputstream = new fileoutputstream(getfilesdir() + "/rreadyreckoner_images/" + filename); byte data[] = new byte[1024]; long total = 0; while ((count = inputstream.read(data)) != -1) { total += count; outputstream.write(data, 0, count); } outputstream.flush(); outputstream.close(); inputstream.close(); } } catch (exception e) { e.printstacktrace(); } return params[0]; } @override protected void onpostexecute(string filename) { log.d("param", filename + " downloaded "); string fname = filename.substring(filename.lastindexof('/') + 1, filename.length()); log.d("length of file : ", string.valueof(lengthoffile)); if (issdpresent) { file f = new file("/storage/emulated/0/rreadyreckoner_images/" + fname); if (f.length() < lengthoffile) { if (f.delete()) { // toast.maketext(rreadysplash.this, "download interrupted please try again!", toast.length_short).show(); log.d("del", "file deleted"); } else { log.d("notdel", "file not deleted"); } } else { // dbhandler.updatedownloadstatus(image_id, "yes"); } } else { file f = new file("/storage/emulated/0/rreadyreckoner_images/" + fname); if (f.length() < lengthoffile) { if (f.delete()) { log.d("del", "file deleted"); } else { log.d("notdel", "file not deleted"); } } else { // dbhandler.updatedownloadstatus(image_id, "yes"); } } } @override protected void onpreexecute() { super.onpreexecute(); } @override protected void onprogressupdate(string... values) { log.d("andro_async", values[0]); } }
any or suggestion appreciated. thank you.
there lot of methods.
for example, can use interface callbacks.
create interface:
public interface mycallback { public void readycallback(int index_thread); }
change class:
class downloadimagesasync extends asynctask<string, string, string> { private int id = 0; private mycallback callback; boolean issdpresent = android.os.environment.getexternalstoragestate().equals(android.os.environment.media_mounted); private string resp; int lengthoffile; public downloadimagesasync(int id, mycallback callback) { this.id = id; this.callback = callback; } @override protected string doinbackground(string... params) { int count; try { url url = new url(params[0]); urlconnection connection = url.openconnection(); connection.connect(); lengthoffile = connection.getcontentlength(); log.d("andro_async", "length of file : " + lengthoffile); string filename = params[0].substring(params[0].lastindexof('/') + 1, params[0].length()); log.d("filename", filename); resp = filename; if (issdpresent) { inputstream inputstream = new bufferedinputstream(url.openstream()); outputstream outputstream = new fileoutputstream("sdcard/rreadyreckoner_images/" + filename); byte data[] = new byte[1024]; long total = 0; while ((count = inputstream.read(data)) != -1) { total += count; outputstream.write(data, 0, count); } outputstream.flush(); outputstream.close(); inputstream.close(); } else { inputstream inputstream = new bufferedinputstream(url.openstream()); outputstream outputstream = new fileoutputstream(getfilesdir() + "/rreadyreckoner_images/" + filename); byte data[] = new byte[1024]; long total = 0; while ((count = inputstream.read(data)) != -1) { total += count; outputstream.write(data, 0, count); } outputstream.flush(); outputstream.close(); inputstream.close(); } } catch (exception e) { e.printstacktrace(); } return params[0]; } @override protected void onpostexecute(string filename) { log.d("param", filename + " downloaded "); if (callback != null) { callback.readycallback(myid); } string fname = filename.substring(filename.lastindexof('/') + 1, filename.length()); log.d("length of file : ", string.valueof(lengthoffile)); if (issdpresent) { file f = new file("/storage/emulated/0/rreadyreckoner_images/" + fname); if (f.length() < lengthoffile) { if (f.delete()) { // toast.maketext(rreadysplash.this, "download interrupted please try again!", toast.length_short).show(); log.d("del", "file deleted"); } else { log.d("notdel", "file not deleted"); } } else { // dbhandler.updatedownloadstatus(image_id, "yes"); } } else { file f = new file("/storage/emulated/0/rreadyreckoner_images/" + fname); if (f.length() < lengthoffile) { if (f.delete()) { log.d("del", "file deleted"); } else { log.d("notdel", "file not deleted"); } } else { // dbhandler.updatedownloadstatus(image_id, "yes"); } } } @override protected void onpreexecute() { super.onpreexecute(); } @override protected void onprogressupdate(string... values) { log.d("andro_async", values[0]); } }
to use class change main function
string[] images = parse_imgjson.image; (int = 0; < images.length; i++) { log.d("image ", images[i]); download_img(images[i], i); } public void download_img(string img_url, int i) { string filename = img_url.substring(img_url.lastindexof('/') + 1, img_url.length()); file file = new file("/storage/emulated/0/rready_images/" + filename); if (file.exists() && !file.isdirectory()) { log.d("image exists", filename); } else { if (filename.contains(".jpg") || filename.contains(".gif") || filename.contains(".png")) { new downloadimagesasync(i, new mycallback() { @override public void readycallback(int index_thread) { //this ready callback } }).execute(img_url); } else { log.d("image download ", "failed " + filename); } } }
do not forget check whether using main ui thread:
if need may wrap updates function:
@override public void readycallback(int index_thread) { //this ready callback runonuithread(new runnable() { @override public void run() { //this ready callback in main ui //i not remember if onpostexecute in main ui thread } }); }
Comments
Post a Comment