Android show progress bar while image loading dynamically

ramesh

In one of my project, I am loading images from the dynamic url. Now I need to show the loading dialogue still all the images loads. I am loading the images using the Async task. I am new to android. Please any one give me a little help

My code looks like.

TableLayout table = (TableLayout)findViewById(R.id.tableLayout_1);
        for(Integer i=0;i<2;i++){


            TableRow rowP = new TableRow(this);
            rowP.setBackgroundColor(Color.parseColor("#FFF000"));




            ImageView image = new ImageView(this);
            String ed="http://www.domain.com/image.jpg";
            image.setTag(ed);
            DownloadImagesTask td=new DownloadImagesTask();
            td.execute(image);

            rowP.addView(image);
            table.addView(rowP);
            }
        }


private class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

    ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(ImageView... imageViews) {
        this.imageView = imageViews[0];
        return download_Image((String)imageView.getTag());
    }

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

    private Bitmap download_Image(String url) {

        Bitmap bmp =null;
        try{
            URL ulrn = new URL(url);
            HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
            InputStream is = con.getInputStream();
            bmp = BitmapFactory.decodeStream(is);
            if (null != bmp)
                return bmp;

            }catch(Exception e){}
        return bmp;
    }
}

Thanks in advance

Edit

private class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

        ImageView imageView = null;
        ProgressDialog dialog;
        Context context;

        public DownloadImagesTask(Context context) {
            this.context = context;
        }

        @Override
        protected Bitmap doInBackground(ImageView... imageViews) {
            this.imageView = imageViews[0];
            return download_Image((String)imageView.getTag());
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            imageView.setImageBitmap(result);
            if (dialog != null)
                dialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = ProgressDialog.show(context, "Title","Message");
        }

        private Bitmap download_Image(String url) {

            Bitmap bmp =null;
            try{
                URL ulrn = new URL(url);
                HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
                InputStream is = con.getInputStream();
                bmp = BitmapFactory.decodeStream(is);
                if (null != bmp)
                    return bmp;

                }catch(Exception e){}
            return bmp;
        }
    }
Chintan Rathod

Add below lines in your DownloadImageTask.

@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialog = ProgressDialog.show(context, "Title",
                "Message");
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);

    if (dialog != null)
        dialog.dismiss();
    //rest of the code
}

and declare ProgressDialog dialog in your DownloadImageTask.

To pass Context, you need to create a contructor for that.

private class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

    ImageView imageView = null;
    ProgressDialog dialog;
    Context context;

    public DownloadImagesTask(Context context) {
        this.context = context;
    }
    //... rest of code ....
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I show a loading image while script is working?

분류에서Dev

Progress bar not updating on Android

분류에서Dev

Android loading listview with progress dialog

분류에서Dev

Displaying progress bar while intent service is running

분류에서Dev

wp8-show default local image as source of image control while Absolute URL image is loading and displayed in listbox

분류에서Dev

Android custom progress bar with .gif file

분류에서Dev

Show HTML code while page is loading

분류에서Dev

Show "loading form" while retrieving data

분류에서Dev

while progress bar is running how to disable other controls of the page in WPF

분류에서Dev

Android Studio refused to show status bar nor Nav Bar

분류에서Dev

Prevent onClick while Activity is displaying Progress Dialog (Android)

분류에서Dev

Getting error while loading image in picture box c#

분류에서Dev

Splash screen with progress bar

분류에서Dev

progress bar not working for me

분류에서Dev

Refresh progress bar

분류에서Dev

ImageView resize dynamically acccording to the image size in android

분류에서Dev

Display Upload Progress in Modal with Bootstrap Progress Bar

분류에서Dev

how to show an image without surfaceflinger on android

분류에서Dev

Dynamically creating and loading JS

분류에서Dev

loading Skin images dynamically

분류에서Dev

Put border in empty progress bar

분류에서Dev

Using an indeterminate progress bar with a listview

분류에서Dev

Progress-bar for Ruby Upload

분류에서Dev

Progress bar at every GridView element

분류에서Dev

Prevent Android Webview to show blank screen while navigating

분류에서Dev

Show loading form

분류에서Dev

youtube-dl file download progress with zenity progress bar

분류에서Dev

Updating progress bar asynchronously using TPL

분류에서Dev

How can I Display a Progress Bar in mplayer?

Related 관련 기사

뜨겁다태그

보관