我有下载Image的AsyncTask,它还显示了进度对话框。 asynctask运行非常精细,并且图像下载的进度也正确显示了值。我通过在doInBackground()中记录下载值然后使用publishProgress()来检查它。但是这个publishProgress不会增加吧progressdialog。
以下是我正在使用的代码。
public class SingleMenuItemActivity extends Activity {
public static final String KEY_TITLE = "title";
public static final String KEY_LINK = "link";
public static final String KEY_DATE = "date";
public static final String TAG = "SingleMenuItemActivity";
private ProgressDialog pDialog;
// Progress dialog type (0 - for Horizontal progress bar)
public static final int PROGRESS_BAR_TYPE = 0;
//public static ImageDownloadTask imTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String name = in.getStringExtra(KEY_TITLE);
String pLink = in.getStringExtra(KEY_LINK);
String pDate = in.getStringExtra(KEY_DATE);
// Displaying all values on the screen
TextView lblTitle = (TextView) findViewById(R.id.title_label);
TextView lblDate = (TextView) findViewById(R.id.publish_label);
lblTitle.setText(name);
lblDate.setText(pDate);
// Set Image
try {
URL url = new URL(pLink);
new ImageDownloadTask().execute(url);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Showing Dialog
* */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case PROGRESS_BAR_TYPE: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading image. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(false);
pDialog.show();
return pDialog;
default:
return null;
}
}
class ImageDownloadTask extends AsyncTask<URL, Float, String> {
public static final String TAG = "ImageDownloadTask";
ImageView imageView = (ImageView) findViewById(R.id.single_list_imageview);
int count;
@Override
protected String doInBackground(URL... params) {
try {
URL url = params[0];
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
float pValue = (float)((total*100)/lenghtOfFile);
Log.d(TAG,"Download so far : "+pValue);
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(pValue);
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result) {
Log.d(TAG,"Bitmap download complete");
dismissDialog(PROGRESS_BAR_TYPE);
// Displaying downloaded image into image view
// Reading image path from sdcard
String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg";
// setting downloaded into image view
imageView.setImageDrawable(Drawable.createFromPath(imagePath));
}
@Override
protected void onCancelled(){
Log.d(TAG,"Progress Dialog was Cancelled");
pDialog.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Things to be done before execution of long running operation. For
// example showing ProgessDialog
showDialog(PROGRESS_BAR_TYPE);
}
protected void onProgressUpdate(Integer... values) {
//super.onProgressUpdate(values);
Log.d(TAG,"values"+values[0]);
// incrementProgressBy(values[0]);
//pDialog.setProgress(values[0]);
setProgress(values[0]);
}
}
}
请告诉我我做错了什么。
谢谢,
答案 0 :(得分:4)
问题在于AsyncTask
的进度类型定义为Float
,而onProgressUpdate()
方法则将Integer
作为参数类型。通过以这种方式声明onProgressUpdate()
,您将重载标准回调方法,而AsyncTask
将不会调用它。如果向此方法添加@Override
注释,则代码也将无法编译。因此,您应该将onProgressUpdate()
的参数类型更改为Float
,或将AsyncTask
的进度类型更改为Integer
,这是自ProgressDialog's
以来更好的解决方案setProgress()
将int
作为参数类型。