一次上传多个图像时,我想显示进度条,该进度条在状态栏通知区域中显示进度条,信息为 1/5 和 2/5 等。其中 1 是没有上传的图像,5 是要上传的图像总数。
在这里我可以在通知区域显示进度条。任何人都可以建议我,如何计算上传(完成)的图像数量以显示进度条(如 1/5)更新。提前致谢。
为了更清楚
我有一个将单个图像上传到服务器的异步任务。但我做不到
1> 计算总图像的大小(例如 5 个图像)
2>如何查找总共上传5张图片的图片数量
private class FileUploadTask extends AsyncTask<Object, Integer,String> {
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(context);
dialog.setMessage("Uploading...");
dialog.setIndeterminate(false);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
dialog.show();
}
@Override
protected String doInBackground(Object... arg0) {
try {
File file = new File("/mnt/sdcard/DCIM/100MEDIA/IMAG0149.jpg");
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
fileInputStream.read(bytes);
fileInputStream.close();
URL url = new URL("http://android.com.bd/form.php");
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
OutputStream outputStream = connection.getOutputStream();
int bufferLength = 1024;
for (int i = 0; i < bytes.length; i += bufferLength) {
int progress = (int)((i / (float) bytes.length) * 100);
Log.i("progress",progress+"dfdf");
publishProgress(progress);
if (bytes.length - i >= bufferLength) {
outputStream.write(bytes, i, bufferLength);
} else {
outputStream.write(bytes, i, bytes.length - i);
}
}
publishProgress(100);
outputStream.close();
outputStream.flush();
InputStream inputStream = connection.getInputStream();
// read the response
inputStream.close();
return "ok";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
dialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String result) {
Log.v("sds", result);
try {
dialog.dismiss();
} catch(Exception e) {
}
}
}