Wednesday, 9 March 2016

Image Download using asynchronous task

hi guys image download using asynchronous very easy. The below coding 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 id



class 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;
    }
}