Tuesday, 16 April 2013

How To download image from URL?

For downloading image from url there are two way :-
 1- using Thread and Handler
 2- using AsyncTask
Here we will use by AsyncTask:-

  1- create AsynkTask

                                    private DownLoader extends AsyncTask<Void,Void,Void>
                                       {
                                         }

 call AsyncTask by passing image url whole code snaps is as:-

  private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        private final ProgressDialog objprogress = new ProgressDialog(
                DealDetailPage.this);
      
        @Override
        protected void onPreExecute() {
            objprogress.setMessage("Loading...");
            objprogress.show();
        }

        @Override
        protected BitmapdoInBackground(String... params) {
            try {
                status = params[1];
                URL feedImage = new URL(params[0]);
                HttpURLConnection conn = (HttpURLConnection) feedImage
                        .openConnection();
                InputStream is = conn.getInputStream();
                android.graphics.BitmapFactory.Options op = new android.graphics.BitmapFactory.Options();
                op.inTempStorage = new byte[16 * 1024];
                op.inSampleSize = 0;
               
                Bitmap imageUser = BitmapFactory.decodeStream(is, null, op);
               

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return imageUser ;
        }
@Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
          // use bitmap here
                           
 }
}