我有一个进度对话框,显示使用字节作为进度单位下载我的文件 我想将进度单位转换为兆字节
public void initializeDialog()
{
this.pDialog = new ProgressDialog(act);
this.pDialog.setMessage("Download");
this.pDialog.setIndeterminate(false);
this.pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.pDialog.setCancelable(true);
}
public AsynchTest() {
initializeDialog();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.setProgressNumberFormat("%1d MB / %2d MB");
pDialog.show();
pDialog.setProgress(0);
}
@Override
protected Void doInBackground(Void... params) {
downloadContent();
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
pDialog.incrementProgressBy((int) byteToMB(progress[0]));
}
@Override
protected void onPostExecute(Void result) {
pDialog.dismiss();
}
我使用此函数将字节转换为 mb
public long byteToMB(long byteTransform)
{
long mb=1024L*1024L;
return byteTransform/mb;
}
我用来更新进度对话框的代码 fragment
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
asynch.publishProgress(count);
output.write(data, 0, count);
}
我像这样设置进度对话框的最大值
sizePDialog+=ConnectionManager.getLength(url);
pDialog.setMax((int) byteToMB(sizePDialog));
但是当我这样做时我遇到了这个错误
04-07 20:09:55.119: E/AndroidRuntime(20166): java.lang.NumberFormatException: Invalid long: "%1d"
非常感谢
最佳答案
我认为你不应该像文档所说的那样在 onProgressUpdate
中调用 setProgressNumberFormat
http://developer.android.com/reference/android/app/ProgressDialog.html#setProgressNumberFormat(java.lang.String)
Change the format of the small text showing current and maximum units of progress. The default is "%1d/%2d". Should not be called during the number is progressing.
事实上,setProgressNumberFormat
仅更改进度和最大进度的显示方式,您无法更改(就像您尝试过的那样)格式化之前的值。
我建议您在将值传输到进度对话框时将字节转换为MB,然后值将被格式化为MB。
这样做,您应该将 pDialog.incrementProgressBy(progress[0]);
替换为 pDialog.incrementProgressBy(byteToMB(progress[0]));
并调用
pDialog.setProgressNumberFormat("%1d MB / %2d MB");
在onPreExecute
关于java - 更改进度对话框的进度单位(字节到兆字节),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15866070/