Android AsyncTask


Android AsyncTask Tutorial and Examples.

Quick Android AsyncTask HowTos and Examples

1. Download Image From Internet via HttpURLConnection

The work will take place in the doInBackground() method. This method gets executed in the background thread.

It's result then gets passed to the onPostExecute() method. The onPostExecute() runs on the main thread so we can use User Interface widgets there.

We will then load our bitmap into an imageview using the setImageBitmap() method.

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Downloads an image asynchronously from the internet
 */
public class DownloadImageTask extends AsyncTask<ImageView, Void, Bitmap> {

    private WeakReference<ImageView> imageView = null;

    /**
     * Download the image
     * @param imageViews image view with tag set to image URL
     * @return the downloaded bitmap
     */
    @Override
    protected Bitmap doInBackground(ImageView... imageViews) {
        this.imageView = new WeakReference<>(imageViews[0]);
        return downloadImage((String)imageView.get().getTag());
    }

    /**
     * Populate the imageView with the downloaded image
     * @param bitmap the downloaded image
     */
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        imageView.get().setImageBitmap(bitmap);
    }

    /**
     * Download the image from the given URL
     * @param urlString the URL to download
     * @return a bitmap of the downloaded image
     */
    private Bitmap downloadImage(String urlString) {
        Bitmap bmp = null;

        try {
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            InputStream is = conn.getInputStream();
            bmp = BitmapFactory.decodeStream(is);
            if (bmp != null) {
                return bmp;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return bmp;
    }
}

Top Android AsyncTask Full Examples

Let's look at some examples.

There are plenty of image downloader libraries.Picasso and Glide are so far my best two.However,the truth is they are third party libraries and have to be added as a dependency in android studio or jar in Eclipse.

Sometimes instead of using third party dependencies you just need to do something using the built in classes.

So in this example we see how to download an image/bitmap in the background thread using asynctask.

AsyncTask is an abstract class that makes threading while updating the UI easier.

Android AsyncTask - Download Image/Bitmap tutorial.

There are plenty of image downloader libraries.Picasso and Glide are so far my best two.However,the truth is they are third party libraries and have to be added as a dependency in android studio or jar in Eclipse.

Sometimes instead of using third party dependencies you just need to do something using the built in classes.

So in this example we see how to download an image/bitmap in the background thread using asynctask.

AsyncTask is an abstract class that makes threading while updating the UI easier.

Android AsyncTask - Update Materialish Progressbar tutorial.

An example of updating a materialish progressbar from AsyncTask .

Android AsyncTask - Update ProgressBar tutorial.

This is android asynctask tutorial.We simply update a progressbar while simulating a background task. Cheers.

Section 1 : MainActivity Class

Our MainActivity  

package com.tutorials.asynctaskprogressbar;

import android.app.Activity;
import an...

There are plenty of image downloader libraries.Picasso and Glide are so far my best two.However,the truth is they are third party libraries and have to be added as a dependency in android studio or jar in Eclipse.When I was writing this example some time ago I was using Eclipse ADT.

Its still a massively fast and I do prefer doing these types of examples with it.However, I hate the process of downloading a jar and addding to Eclipse.So In this tutorial we talk Android AsyncTask and Bitmaps.How to download image and show it in a simple gridview. Cheers.    

This is android asynctask tutorial.We simply update a progressbar while simulating a background task.

Best Regards, Oclemy.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post