简体   繁体   English

显示进度条,直到从服务器下载图像

[英]showing progressbar until image downloaded from server

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... 亲爱的,我能够从url显示图像,意味着url存储在一个数组中,并且我有两个按钮“ next”,“ back”,以便当我单击按钮图像时一个接一个地从服务器下载图像作为响应,现在我想要在从服务器下载图像时显示进度条,并且当​​响应出现时,进度条会自动隐藏,状态显示progreebar,直到图像未显示在屏幕上并且图像显示progreebar活动完成时才显示响应...所以该怎么做用于在按钮单击上一个接一个显示图像的代码如下...如果可能,请在我的代码中进行修改bcoz我也是android和java中的新手。。。

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);
  } 

}

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();
                        }

                    }
}

Use the concept of Handler in your code to do this. 在代码中使用Handler的概念可以做到这一点。

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();
            }
        }
 }

for show progressbar until image will download try to use asyncTask method. 对于show progressbar,直到将下载图像为止,请尝试使用asyncTask方法。

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; 创建局部变量ProgressBar栏; replace this line new ImageDownload().execute(imageURL); 替换此行new ImageDownload().execute(imageURL); instead of loadImage(imageURL); 而不是loadImage(imageURL);

for more info for asyncTask then go here. 有关asyncTask的更多信息, 请转到此处。

Check when image gets download from server, place ImageView and progressdialog at same place, First set progressdialog visibile--> true and imageview visibility --> false. 检查何时从服务器下载图像,将ImageView和progressdialog放在同一位置,首先设置progressdialog可见-> true和imageview可见性-> 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 然后设置进度对话框的可见性-> false和imageview可见性-> true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 进度条,直到从服务器加载数据 - Progressbar until the load of data from server Android-在imageView中显示进度对话框(作为空白视图),直到从服务器下载图像 - Android - show progress dialog in imageView (as empty view) until image is downloaded from server 我将图像附加到正在从服务器下载的回收器视图中,但是回收器视图未显示任何图像 - I am attaching a image to a recycler view which is being downloaded from server, but recycler view is not showing any image 从网址下载的图片未显示在视图中 - downloaded image from url is not showing in view 显示进度条,直到执行多个异步任务 - Showing progressbar until multiple async tasks are executed 从android上的服务器下载的图片返回null - image downloaded from server on android returning null Android 显示图库中的图像,需要从 Internet 下载 - Android showing Image in Gallery which needs to be downloaded from Internet 下载的图像未显示在画廊 android - downloaded image not showing in galllery android 从android中的服务器下载图像后图像方向发生变化 - Image orientation changes after downloaded image from server in android 从fragemnet中显示进度条 - Showing a progressbar from within a fragemnet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM