2 jonskeet JonSkeet 于 2013.04.08 13:09 提问

Android:显示进度对话框

我在做一个 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;

}

async task class :

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

在下面的 adapter 类中调用:

 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幅图像,但是现在只显示最后一幅图像,进度对话框进入无限循环,如何处理?

1个回答

curie_87
curie_87   2013.04.09 17:25

我的解决方案是:

class DownloadFileAsync extends AsyncTask<String, String, String>
{
    int count;
    URL myFileUrl;
    ImageView imageview;
    Bitmap bp;

    public DownloadFileAsync(ImageView iv, URL uu)
    {
        this.imageview = iv;
        this.myFileUrl = uu;
    }
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_PROGRESS);
    }
    @Override
    protected String doInBackground(String... aurl)
    {
        for (int i = 0; i < aurl.length; i++)
            System.out.println("----------" + i + "------" + aurl[i]);

        try
        {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            conn.connect();
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is);
            bp = BitmapFactory.decodeStream((InputStream) (myFileUrl).getContent());
            bp = Bitmap.createScaledBitmap(bp, 70, 70, true);

        }
        catch (Exception e)
        {
            System.out.println(e);
            e.printstacktrace();
        }
        return null;
    }
    protected void onProgressUpdate(String... progress)
    {
        dialog.setProgress(Integer.parseInt(progress[0]));
    }
    @Override
    protected void onPostExecute(String unused)
    {
        try
        {
            imageview.setImageBitmap(bp);
            System.out.println("this is" + this);
            // dialog.dismiss();
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!