Image Download Asynchronously using Android
AsyncTask enables you to implement Multiple Threading without getting your hands dirty into thread.
AsyncTask is very easy to use, and it allowing background operation in dedicated thread and passing the result back to UI thread. For example if you going to download image go ahead and use AsynTask
- AsynTask defined by basic three generic types.
- Params
- Progress
- Results
4 Steps
- onPreExecute
- doInBackround
- onProgressUpdate
- onPostExecute
- OnPreExecute() you can define the code, which need to be executed before background processing starts.
- doinBackround() method contains the code which need to be executed in backround, we can send result to multiple times to event threat by publishProgress() method, to notify background processing has beeb completed we can send result simply.
- The onProgressUpdate() method receive the progress update from the doinBackground method. Which is published via publishProgress.
- The onPostExecute() method handle the result returned by the doinBackground method.
-
The following code is very useful to all.AsyncTaskRunnerObject runner = new AsyncTaskRunnerObject(this);runner.execute(Partnerdownloadurl,userid);//update//Partnerdownloadurl = Download url (passing parameter )//userid = userid is the login member idclass AsyncTaskRunnerObject extends AsyncTask<String,String,String> {private String resp,Memberid,AsyType,OBJID;private Context myCtx;public AsyncTaskRunnerObject(Context ctx){this.myCtx = ctx;}@Override protected void onPreExecute() {Log.e("Pre Execute","Pre Execute");}@Override protected void onPostExecute(String s) {Log.e("Post Execute",""+s);StorePreference.SinglewritePreference(myCtx, "LOCAL_FEMALE_PROFILE_IMAGE", s);}@Override protected String doInBackground(String... params) {HttpURLConnection urlConnection = null;try {URL uri = new URL(params[0]);Memberid = params[1];urlConnection = (HttpURLConnection) uri.openConnection();urlConnection.setRequestMethod("GET");int statusCode = urlConnection.getResponseCode();if (statusCode != HttpStatus.SC_OK) {return null;}InputStream inputStream = urlConnection.getInputStream();if (inputStream != null) {File storageDir;String mediatype = null;String audiofmt = null;mediatype = "Profile";audiofmt = ".jpg";if (Environment.getExternalStorageState() == null || Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {storageDir = new File(Environment.getDataDirectory(), mediatype);} else {storageDir = new File(Environment.getExternalStorageDirectory() + Common.STORAGE_PATH, mediatype);}if (!storageDir.exists() && !storageDir.mkdirs()) {// For HTC Device storageDir = new File(Common.STORAGE_PATH, mediatype);if (!storageDir.exists() && !storageDir.mkdirs()) {}}File image = File.createTempFile(Memberid, /* prefix */audiofmt, /* suffix */storageDir /* directory */);FileOutputStream f = new FileOutputStream(image);byte[] buffer = new byte[1024];int len1 = 0;while ((len1 = inputStream.read(buffer)) > 0) {f.write(buffer, 0, len1);}f.close();resp=image.getAbsolutePath();return resp;}} catch (Exception e) {Log.d("URLCONNECTIONERROR", e.toString());if (urlConnection != null) {urlConnection.disconnect();}} finally {if (urlConnection != null) {urlConnection.disconnect();}}return resp;}}