Android:显示进度对话框
我正在制作一个更新的android应用程序。为此,我需要一个简单的功能,可以下载一个文件,并显示在ProgressDialog当前的进展。我知道如何下载文件,但不确定如何显示当前进度。我使用以下方法下载图像Android:显示进度对话框,android,progressdialog,Android,Progressdialog,我正在制作一个更新的android应用程序。为此,我需要一个简单的功能,可以下载一个文件,并显示在ProgressDialog当前的进展。我知道如何下载文件,但不确定如何显示当前进度。我使用以下方法下载图像 public Bitmap DownloadFile(String url){ URL myFileUrl; Bitmap bitmap=null; try { myFileUrl = new URL(imageUrl); HttpURLConnection conn= (HttpU
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]);
}
}
我需要下载9个图片,但我面临的问题是,它只显示最后一个图片和进度对话框进入无限循环
谁能告诉我如何解决thios问题
谢谢如果您还记得的话,我已经建议您使用之前的方法解决您的问题
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();
}
}
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
//TODO下载文件在这里
返回true;
}
受保护的void onProgressUpdate(整数…进度){
//此处无事可做,您没有下载详细信息
}
受保护的void onPostExecute(布尔结果){
MyActivity.this.hideProgressDialog();
}
}
好的,我在这里看到两个潜在问题
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;
}
还有这里的异步任务。我创建了一个构造函数并将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]);
}
}
public类BackgroundAsyncTask扩展了AsyncTask{
int-myProgress;
ImageView iv;
公共背景异步任务(ImageView ImageView){
iv=图像视图;
}
@凌驾
受保护的void onPostExecute(位图结果){
dialog.dismise();
iv.设置可见性(视图可见);
iv.设置图像位图(结果);
System.out.println(“BBBBB”);
System.out.println(“后执行”);
}
@凌驾
受保护的void onPreExecute(){
System.out.println(“预执行”);
dialog=ProgressDialog.show(ProfileNormalUserPhotos.this,“正在加载…”,“请稍候…”);
}
@凌驾
受保护位图doInBackground(字符串…路径){
System.out.println(imageUrl+“imageUrl”);
返回下载文件(imageUrl);
}
@凌驾
受保护的void onProgressUpdate(整型…值){
//TODO自动生成的方法存根
progressBar.setProgress(值[0]);
}
}
但我仍然不能保证无限循环的解决方案,但解决一些其他问题总是好的:)
HTH.我通过研究得到了解决方案,以下应该是解决方案:
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);
}
}
}
class DownloadFileAsync扩展了AsyncTask
{
整数计数;
URL myFileUrl;
图像视图图像视图;
位图bp;
公共下载文件异步(ImageView iv,URL uu)
{
this.imageview=iv;
this.myFileUrl=uu;
}
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
showDialog(对话框下载进度);
}
@凌驾
受保护的字符串背景(字符串…aurl)
{
for(int i=0;i
我已经这样做了,但我面临的问题是进度对话框进入无限循环。为什么是无限循环?只需检查onPostExecute()中是否有(dialog.isShowing()){dialog.disease();}是的,它在onPostExecute()方法中是的,可能是的,但有些要点值得注意,比如将imageView作为参数传递。那么,还应该传递什么呢?只有在我的代码中已经完成的图像视图和url。
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]);
}
}
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);
}
}
}