Chrisss
Chrisss

Reputation: 107

How to detect the loading of the image from the Internet - progress bar

I have a class:

    public class DownloadImageFromInternet extends AsyncTask<String, Void, Bitmap> {
    private ImageView imageView;

    public DownloadImageFromInternet(ImageView imageView) {
        this.imageView = imageView;
    }

    protected Bitmap doInBackground(String... urls) {
        String imageURL = urls[0];
        Bitmap bimage = null;
        try {
            InputStream in = new java.net.URL(imageURL).openStream();
            bimage = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error Message", e.getMessage());
            e.printStackTrace();
        }
        return bimage;
    }

    protected void onPostExecute(Bitmap result) {
        imageView.setImageBitmap(result);
    }

}

I want to create a progress bar that will detect the loading of the image I download from the internet. How to do it?

Upvotes: 0

Views: 61

Answers (1)

Paresh
Paresh

Reputation: 6857

This is how it's done:

public class DownloadImageFromInternet extends AsyncTask<String, Void, Bitmap> {
    private ImageView imageView;
    private ProgressDialog pd;

    public DownloadImageFromInternet(ImageView imageView) {
        this.imageView = imageView;
    }

    protected void onPreExecute() {
        pd = new ProgressDialog(); //Create dialog
        pd.show(); // show dialog
    }

    protected Bitmap doInBackground(String... urls) {
        ...
        ..
        return bimage;
    }

    protected void onPostExecute(Bitmap result) {
        pd.dismiss();
        ...
    }
}

Upvotes: 1

Related Questions