showing progressbar until image downloaded from se

2019-07-25 17:15发布

问题:

dear i am able to showing image from urls,means urls are stored in a an array and i have two button "next","back "for seeing image one after another when i click on button image are downloaded from server as response now i want to showing progress bar when image going downloade from server and when response come then progress bar hide automatically menas show progreebar untill response as a image not showin on screen and when image display progreebar activity finishes...so how to do this my piece of code for showing image one after another on button click are below...please modify in my code if possible bcoz i am new in android and java as well..thans a lot in advance...

public class artspacedetailShowingNow extends Activity implements OnClickListener {
private int imageCounter = 0;
private ImageView imageLoader;
private String[] imageList = {"http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_di_girolamo._diosas/198915-1-esl-AR/MARTIN_DI_GIROLAMO._Diosas.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/jorge_macchi._la_espera/198929-1-esl-AR/JORGE_MACCHI._La_espera.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/leon_ferrari._hongo_nuclear/198950-1-esl-AR/LEON_FERRARI._Hongo_Nuclear.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_sastre._fiebre/198922-1-esl-AR/MARTIN_SASTRE._Fiebre.jpg"};
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showingnow);
       imageLoader = (ImageView) findViewById(R.id.imageLoader);
     Button next = (Button) findViewById(R.id.next);
    Button back = (Button) findViewById(R.id.back);
    next.setOnClickListener(this);
    back.setOnClickListener(this);
    back.setEnabled(false);
    this.loadImage(imageList[imageCounter]);

}
@Override
 public void onClick(View v)
{
String imagePath = null;
  switch (v.getId())
{
case R.id.next:
    Log.i("Tag","tag");
    if(imageCounter < imageList.length)
    {
        imageCounter++;
        imagePath = imageList[imageCounter];
        if (imageCounter==(imageList.length)-1)
        {
            { 
                Button next=(Button)findViewById(R.id.next); 
               next.setEnabled(false); 
             }
        }
        else
        {
            Button back=(Button)findViewById(R.id.back); 
            back.setEnabled(true); 

        }
    }
    break;
case R.id.back:
    if(imageCounter > 0)
    {
        imageCounter--;
        imagePath = imageList[imageCounter];
        if (imageCounter==0) 
        { 
            Button back=(Button)findViewById(R.id.back); 
            back.setEnabled(false); 

        }
        else
        {
            Button next=(Button)findViewById(R.id.next); 
             next.setEnabled(true);

        }
    }
    break;
}
this.loadImage(imagePath);
}
private void loadImage(String imagePath)
{   
  try {
      URL aURL = new URL(imagePath);
      URLConnection conn = aURL.openConnection();
      conn.connect();
      InputStream is = conn.getInputStream();
      BufferedInputStream bis = new BufferedInputStream(is);
     Bitmap bm = BitmapFactory.decodeStream(bis);

      imageLoader.setImageBitmap(bm);
      imageLoader.setImageBitmap(bm);
  } 

}

回答1:

check this..

public class artspacedetailShowingNow extends Activity implements OnClickListener {
private int imageCounter = 0;
private ImageView imageLoader;
private ProgressDialog bar;
private String[] imageList = {"http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_di_girolamo._diosas/198915-1-esl-AR/MARTIN_DI_GIROLAMO._Diosas.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/jorge_macchi._la_espera/198929-1-esl-AR/JORGE_MACCHI._La_espera.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/leon_ferrari._hongo_nuclear/198950-1-esl-AR/LEON_FERRARI._Hongo_Nuclear.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_sastre._fiebre/198922-1-esl-AR/MARTIN_SASTRE._Fiebre.jpg"};
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showingnow);
       imageLoader = (ImageView) findViewById(R.id.imageLoader);
     Button next = (Button) findViewById(R.id.next);
    Button back = (Button) findViewById(R.id.back);
    next.setOnClickListener(this);
    back.setOnClickListener(this);
    back.setEnabled(false);
    new ImageDownload().execute(imageList[imageCounter]); 
    //this.loadImage(imageList[imageCounter]);

}
@Override
 public void onClick(View v)
{
String imagePath = null;
  switch (v.getId())
{
case R.id.next:
    Log.i("Tag","tag");
    if(imageCounter < imageList.length)
    {
        imageCounter++;
        imagePath = imageList[imageCounter];
        if (imageCounter==(imageList.length)-1)
        {
            { 
                Button next=(Button)findViewById(R.id.next); 
               next.setEnabled(false); 
             }
        }
        else
        {
            Button back=(Button)findViewById(R.id.back); 
            back.setEnabled(true); 

        }
    }
    break;
case R.id.back:
    if(imageCounter > 0)
    {
        imageCounter--;
        imagePath = imageList[imageCounter];
        if (imageCounter==0) 
        { 
            Button back=(Button)findViewById(R.id.back); 
            back.setEnabled(false); 

        }
        else
        {
            Button next=(Button)findViewById(R.id.next); 
             next.setEnabled(true);

        }
    }
    break;
}
 new ImageDownload().execute(imagePath);
//this.loadImage(imagePath);
}
private void loadImage(String imagePath)
{   
  try {
      URL aURL = new URL(imagePath);
      URLConnection conn = aURL.openConnection();
      conn.connect();
      InputStream is = conn.getInputStream();
      BufferedInputStream bis = new BufferedInputStream(is);
     Bitmap bm = BitmapFactory.decodeStream(bis);

      imageLoader.setImageBitmap(bm);
      imageLoader.setImageBitmap(bm);
  }

private class ImageDownload extends AsyncTask<String , Void, Void>(){

                        @Override
                        protected Void doInBackground(String... params) {
                            loadImage(params[0]);
                            return null;
                        }
                        @Override
                        protected void onPostExecute(Void result) {

                            bar.dismiss();
                            super.onPostExecute(result);
                        }
                        @Override
                        protected void onPreExecute() {
                            bar = new ProgressDialog(activity);
                            bar.setMessage("Processing...");
                            bar.setIndeterminate(true);
                            super.onPreExecute();
                        }

                    }
}


回答2:

Use the concept of Handler in your code to do this.

here is the code..

ProgressDialog _progressDialog = ProgressDialog.show(this,"Saving Data","Please wait......");
settintAdater();

 private void settingAdater(){

        Thread _thread = new Thread(){

            public void run() {

                Message _msg = new Message();
                _msg.what = 1; 
                 // Do your task where you want to rerieve data to set in adapet
                YourCalss.this._handle.sendMessage(_msg);
            };
        };
        _thread.start();
    }
 Handler _handle = new Handler(){

        public void handleMessage(Message msg) {

            switch(msg.what){

                case 1:
                    _progressDialog.dismiss();
                     listview.setAdapter();
            }
        }
 }


回答3:

for show progressbar until image will download try to use asyncTask method.

here sample code for it.

private class ImageDownload extends AsyncTask<String , Void, Void>(){

                        @Override
                        protected Void doInBackground(String... params) {
                            loadImage(params[0]);
                            return null;
                        }
                        @Override
                        protected void onPostExecute(Void result) {

                            bar.dismiss();
                            super.onPostExecute(result);
                        }
                        @Override
                        protected void onPreExecute() {
                            bar = new ProgressDialog(activity);
                            bar.setMessage("Processing...");
                            bar.setIndeterminate(true);
                            super.onPreExecute();
                        }

                    }

create local variable ProgressBar bar; replace this line new ImageDownload().execute(imageURL); instead of loadImage(imageURL);

for more info for asyncTask then go here.



回答4:

Check when image gets download from server, place ImageView and progressdialog at same place, First set progressdialog visibile--> true and imageview visibility --> false. then start downloading image from server, use handler and get to know when image available from server. and that time set progressdialog's visibility --> false and imageview visibility --> true