android - Achieving Concurrency using AsyncTask? -
i developing android application download images web server. code running fine. using asynctask download images sdcard.
i on 4mbps connection application taking 8 mins download 3 images (2.5 mb). have read else asynctask automatically manages thread creation, can achieve concurrency ?
i posting code below. code below asynctask activity downloads image server sdcard.
public class bitmapdownloadertask extends asynctask<string, void, bitmap> { private string url; bitmap bitmap1; string sdcard; private final weakreference<imageview> imageviewreference; public bitmapdownloadertask(imageview imageview) { imageviewreference = new weakreference<imageview>(imageview); } @override // actual download method, run in task thread protected bitmap doinbackground(string... params) { // params comes execute() call: params[0] url. bitmap1 = downloadbitmap(params[0]); boolean avail = ismemorysizeavailableandroid(bitmap1.getrowbytes(), environment.isexternalstorageemulated()); if (avail) { try { sdcard = environment.getexternalstoragedirectory().tostring() + "/mycatalogue"; file f1 = new file(sdcard); if (!f1.exists()) { f1.mkdirs(); } string filename1 = params[0].substring(params[0] .lastindexof("/") + 1); file file1 = new file(f1.tostring(), filename1); outputstream stream1 = new fileoutputstream(file1); bitmap1.compress(compressformat.jpeg, 100, stream1); log.w("abhishek", "card " + sdcard); } catch (exception e) { e.printstacktrace(); } } log.w("imagedownloader", "success bitmap is" + bitmap1); return downloadbitmap(params[0]); } protected static boolean ismemorysizeavailableandroid(long download_bytes, boolean isexternalmemory) { boolean ismemoryavailable = false; long freespace = 0; // if isexternalmemory true calculate external sd card available // size if (isexternalmemory) { try { statfs stat = new statfs(environment .getexternalstoragedirectory().getpath()); freespace = (long) stat.getavailableblocks() * (long) stat.getblocksize(); if (freespace > download_bytes) { ismemoryavailable = true; } else { ismemoryavailable = false; } } catch (exception e) { e.printstacktrace(); ismemoryavailable = false; } } else { // find phone available size try { statfs stat = new statfs(environment.getdatadirectory() .getpath()); freespace = (long) stat.getavailableblocks() * (long) stat.getblocksize(); if (freespace > download_bytes) { ismemoryavailable = true; } else { ismemoryavailable = false; } } catch (exception e) { e.printstacktrace(); ismemoryavailable = false; } } return ismemoryavailable; } @override // once image downloaded, associates imageview protected void onpostexecute(bitmap bitmap) { if (iscancelled()) { bitmap = null; } if (imageviewreference != null) { imageview imageview = imageviewreference.get(); if (imageview != null) { imageview.setimagebitmap(bitmap); } } } static bitmap downloadbitmap(string url) { final androidhttpclient client = androidhttpclient .newinstance("android"); final httpget getrequest = new httpget(url); try { httpresponse response = client.execute(getrequest); final int statuscode = response.getstatusline().getstatuscode(); if (statuscode != httpstatus.sc_ok) { log.w("imagedownloader", "error " + statuscode + " while retrieving bitmap " + url); return null; } else { log.w("imagedownloader", "success " + statuscode + " while retrieving bitmap " + url); } final httpentity entity = response.getentity(); if (entity != null) { inputstream inputstream = null; try { inputstream = entity.getcontent(); final bitmap bitmap = bitmapfactory .decodestream(inputstream); return bitmap; } { if (inputstream != null) { inputstream.close(); } entity.consumecontent(); } } } catch (exception e) { // provide more explicit error message ioexception or // illegalstateexception getrequest.abort(); log.w("imagedownloader", "error while retrieving bitmap " + url); } { if (client != null) { client.close(); } } return null; } }
use executeonexecutor
http://developer.android.com/reference/java/util/concurrent/executor.html
new bitmapdownloadertask.executeonexecutor(asynctask.thread_pool_executor, "your urls");
quoting docs
http://developer.android.com/reference/android/os/asynctask.html
when first introduced, asynctasks executed serially on single background thread. starting donut, changed pool of threads allowing multiple tasks operate in parallel. starting honeycomb, tasks executed on single thread avoid common application errors caused parallel execution.
if want parallel execution, can invoke executeonexecutor(java.util.concurrent.executor, object[]) thread_pool_executor.
Comments
Post a Comment