2011-10-24 49 views
3

我正在制作一个更新的android应用程序。为此,我需要一个简单的函数,可以下载一个文件并在ProgressDialog中显示当前的进度。我知道如何下载文件,但我不知道如何显示当前的进度。我正在使用以下方法下载图像。Android:显示进度对话框

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问题。

感谢

回答

3

我通过它的工作得到了解决和下面应该是这样的解决方案:

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

我已经建议你,如果你还记得使用AsyncTask之前为你的问题。

  1. onPreExecute() - 显示proress对话框
  2. doInBackground() - 调用doInBackground内的DownloadFile()方法()
  3. 驳回进度对话框。

通过这个例子你去了解它,并在你的方式实现它:AsyncTask with Progress Dialog

+0

我已经做到了,但我面临的问题是,进度对话框进入无限循环。 – ekjyot

+0

为什么无限?只要检查是否(dialog.isShowing()){dialog.dismiss(); }里面的onPostExecute() –

+0

是的,它是在onPostExecute()方法 – ekjyot

0

你必须使用一个AssyncTask能够很容易沟通进展到UI线程。

对于对话框中,使用ProgressDialog

private void downloadFile(URL fileUrl) { 
    if (myProgressDialog == null) { 
     new DownloadFilesTask().execute(fileUrl); 
     myProgressDialog = ProgressDialog.show(this, "Downloading file " + fileUrl.toString(), "Wait patiently, your download is in progress.", true /*You wont have a time estimate*/, false /*Download cannot be canceled*/); 
    } else { 
     Log.e(LOG_TAG, "A download is already in progress"); 
    } 
} 

private void hideDialog() { 
    if (myProgressDialog != null) { 
     myProgressDialog.cancel(); 
    } 
} 

而对于AssycTask,在活动中使用一个内部类:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Boolean> { 
    protected Long doInBackground(URL... urls) { 
     // TODO Download file here 
     return true; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     // TODO nothing to do here, you don't have download details 
    } 

    protected void onPostExecute(Boolean result) { 
     MyActivity.this.hideProgressDialog(); 
    } 
} 
0

好吧,我在这里看到2层潜在的问题..

  1. 您必须为您的适配器使用支架类结构。

  2. 在asyncTask中,imageView应该不同于listView中的每个项目。基本上,您必须将imageView作为参数传递给asyncTask。

我改变了你的适配器,看看它。

static class ViewHolder { 

      TextView tv; 
      ImageView iv; 

    } 


    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder vh; 
     View v = null; 

     if(convertView==null){ 

        vh = new ViewHolder(); 
        LayoutInflater li = getLayoutInflater(); 
        v = li.inflate(R.layout.icon, null); 
        vh.tv = (TextView)v.findViewById(R.id.text); 
        vh.iv= (ImageView)v.findViewById(R.id.image); 
        v.setTag(vh); 
     } 
     else 
     { 
      vh = (ViewHolder) convertView.getTag(); 
      v = convertView; 
     } 

     vh.tv.setText("Profile Image "+(position+1)); 
     imageUrl = "http://ondamove.it/English/images/users/"; 
     imageUrl=imageUrl+stringOnTextView[position]; 
     new BackgroundAsyncTask(vh.iv).execute(imageUrl); 

     return v; 
    } 

而且这里还有asyncTask。我做了一个构造函数并将imageView作为参数传递。

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

      int myProgress; 
      ImageView iv; 

      public BackgroundAsyncTask (ImageView imageView) { 
       iv = imageView; 
      } 

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

     } 

,但我仍然不能保证解决无限循环,但它始终是好解决一些其他问题:)

HTH。

+1

内部很抱歉说,但它没有帮助.. – ekjyot

+0

是的,可能是,但有些值得注意,如传递imageView作为参数。 –

+0

那么还有什么应该通过 – ekjyot