I am displaying an image from a server in Android. For that I have gone through the tutorial Android Load Image From URL Example. It is very helpful. But it is taking 5 minutes to display the image from the server. So I want to display the image asynchronously. How can I do that?
            Asked
            
        
        
            Active
            
        
            Viewed 2,546 times
        
    0
            
            
        - 
                    You should try [Android - Universal Image Loader](https://github.com/nostra13/Android-Universal-Image-Loader) or [ImageLoader given in Lazy Loading ListView example](http://stackoverflow.com/a/3068012/379693) – Paresh Mayani Jun 05 '12 at 05:21
1 Answers
2
            
            
        The below snippets will help you.
DownloadHelper.java
public interface DownloadHelper
{
    public void OnSucess(Bitmap bitmap);
    public void OnFailure(String response);
}
MainActivity.java
public class GalleryExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        DownloadHelper downloadHelper = new DownloadHelper()
        {
            @Override
            public void OnSucess(Bitmap bitmap)
            {
                ImageView imageView=(ImageView)findViewById(R.id.imageView);
                imageView.setImageBitmap(bitmap);
            }
            @Override
            public void OnFailure(String response)
            {
                Toast.makeText(context, response, Toast.LENGTH_LONG).show();
            }
        };
        new MyTask(this,downloadHelper).execute("image url");
    }
MyTask.java
public class DownloadTask extends AsyncTask<String, Integer, Object>
{
    private Context context;
    private DownloadHelper downloadHelper;
    private ProgressDialog dialog;
    public DownloadTask(Context context,DownloadHelper downloadHelper)
    {
        this.context = context;
    }
    @Override
    protected void onPreExecute()
    {
        dialog = new ProgressDialog(context);
        dialog.setTitle("Please Wait");
        dialog.setMessage("Fetching Data!!");
        dialog.setCancelable(false);
        dialog.show();
        super.onPreExecute();
    }
    @Override
    protected Object doInBackground(String... params)
    {
        URL aURL = new URL(myRemoteImages[position]);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        /* Decode url-data to a bitmap. */
        Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
        return bm;
    }
    @Override
    protected void onPostExecute(Object result)
    {
        dialog.dismiss();
        if (result != null)
        {
            downloadHelper.OnSucess((Bitmap)result);
        } 
        else
        {
            downloadHelper.OnFailure("Error in Downloading Data!!");
        }
        super.onPostExecute(result);
    }
}
 
    
    
        Heidelbergensis
        
- 475
- 2
- 5
- 18
 
    
    
        Vipul
        
- 27,808
- 7
- 60
- 75
