Android:显示进度对话框

Android:显示进度对话框,第1张

概述我正在制作一个更新的 Android应用程序.为此,我需要一个简单的函数,可以下载文件并在ProgressDialog中显示当前进度.我知道如何下载文件,但我不知道如何显示当前进度.我使用以下方法下载图像. public Bitmap DownloadFile(String url){URL myFileUrl;Bitmap bitmap=null;try {myFileUrl = new 我正在制作一个更新的 Android应用程序.为此,我需要一个简单的函数,可以下载文件并在ProgressDialog中显示当前进度.我知道如何下载文件,但我不知道如何显示当前进度.我使用以下方法下载图像. @H_419_7@

@H_419_7@

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,true);    System.out.println("name of butmap"+bitmap.toString());} catch (Exception e) {// Todo auto-generated catch blocke.printstacktrace();}return bitmap;}
@H_419_7@以下是我的异步任务类:

@H_419_7@

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");    }@OverrIDeprotected voID onPreExecute() {    System.out.println("pre execute");    dialog = ProgressDialog.show(ProfilenormalUserPhotos.this,"Loading...","Please wait...");}@OverrIDeprotected Bitmap doInBackground(String...paths) {    System.out.println(imageUrl+"  imageurl");    return Downloadfile(imageUrl);    }@OverrIDeprotected voID onProgressUpdate(Integer... values) {    // Todo auto-generated method stub        progressbar.setProgress(values[0]);}}
@H_419_7@我正在调用以下适配器类中的方法:

@H_419_7@

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;    }}
@H_419_7@我需要下载9张图片,但我遇到的问题是它只显示最后一张图像,进度对话框进入无限循环.

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

@H_419_7@谢谢

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

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

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

总结

以上是内存溢出为你收集整理的Android:显示进度对话框全部内容,希望文章能够帮你解决Android:显示进度对话框所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1121873.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存