当前位置 : 主页 > 手机开发 > android >

Android:显示进度对话框

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在制作一个更新的 Android应用程序.为此,我需要一个简单的函数,可以下载文件并在ProgressDialog中显示当前进度.我知道如何下载文件,但我不知道如何显示当前进度.我使用以下方法下载
我正在制作一个更新的 Android应用程序.为此,我需要一个简单的函数,可以下载文件并在ProgressDialog中显示当前进度.我知道如何下载文件,但我不知道如何显示当前进度.我使用以下方法下载图像.

public Bitmap DownloadFile(String url){
URL myFileUrl;
Bitmap bitmap=null;
try {
myFileUrl = new URL(imageUrl);
    HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
    conn.setDoInput(true);
    conn.setConnectTimeout(10000);
    conn.setReadTimeout(10000);
    conn.connect();
    InputStream is = conn.getInputStream();
    bmImg = BitmapFactory.decodeStream(is);
    bitmap = BitmapFactory.decodeStream((InputStream) new URL(  
         imageUrl).getContent()); 
    bitmap = Bitmap.createScaledBitmap(bitmap,80 , 80, true);
    System.out.println("name of butmap"+bitmap.toString());

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;

}

以下是我的异步任务类:

public class BackgroundAsyncTask extends AsyncTask<String, Integer, Bitmap> {

   int myProgress;

   @Override

    protected void onPostExecute(Bitmap result) {
    dialog.dismiss();
    iv.setVisibility(View.VISIBLE);
    iv.setImageBitmap(result);
    System.out.println("bbbbbbbbb");
    System.out.println("post execute");
    }

@Override
protected void onPreExecute() {
    System.out.println("pre execute");
    dialog = ProgressDialog.show(ProfileNormalUserPhotos.this, "Loading...", "Please wait...");

}

@Override
protected Bitmap doInBackground(String...paths) {
    System.out.println(imageUrl+"  imageurl");
    return DownloadFile(imageUrl);

    }
@Override
protected void onProgressUpdate(Integer... values) {
    // TODO Auto-generated method stub
        progressBar.setProgress(values[0]);
}

}

我正在调用以下适配器类中的方法:

public class ImageAdapter extends BaseAdapter {  
       Context mContext;
   public String[] stringOnTextView;

     public ImageAdapter(Context c) {  
       mContext = c;  
     }  

public ImageAdapter(Context Context,
    String[] stringOnTextView) {
    this.mContext=Context;
    this.stringOnTextView=stringOnTextView;
}

public int getCount() {  
      return stringOnTextView.length;  
     }  


public Object getItem(int position) {  
      return null;  
     }  

     public long getItemId(int position) {  
      return position;  
     }  


     public View getView(int position, View convertView, ViewGroup parent) {
         View v = null;
        if(convertView==null){

            try{

            {
                  LayoutInflater li = getLayoutInflater();
                  v = li.inflate(R.layout.icon, null);
                  TextView tv = (TextView)v.findViewById(R.id.text);
                  tv.setText("Profile Image "+(position+1));
                   iv= (ImageView)v.findViewById(R.id.image);

                   imageUrl = "http://ondamove.it/English/images/users/";
                  imageUrl=imageUrl+stringOnTextView[position];
                   new BackgroundAsyncTask().execute(imageUrl);


                   }
           }catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }
        }
        else
        {
            try{
            v = convertView;}
            catch(Exception e){
                System.out.println(e);
            }
        }
        return v;
    }
}

我需要下载9张图片,但我遇到的问题是它只显示最后一张图像,进度对话框进入无限循环.

任何人都可以告诉我如何解决thios问题.

谢谢

如果你记得的话,我已经建议你先前使用 AsyncTask来解决你的问题.

> onPreExecute() – 显示proress对话框
> doInBackground() – 在doInBackground()中调用你的DownloadFile()方法
>关闭进度对话框.

通过这个例子来理解它并按照你的方式实现它:AsyncTask with Progress Dialog

网友评论