【发布时间】:2014-05-13 18:46:05
【问题描述】:
我已经非常彻底地完成了我的研究,但没有实际结果,所以我在这里。
我的应用程序中有一个水平进度条,用于指示文件上传的进度。但是,当我上传整个目录时,我试图让它显示当时上传的单个文件的进度。我使用带有 FixedThreadPool(1) 的异步任务。问题是,只有第一个任务会触发进度条。
主要活动中的方法:
uploadExecutor = Executors.newFixedThreadPool(1);
private void upload() throws DropboxException, IOException {
if(isDir){
DataDir dir = new DataDir(file);
List<File> files = dir.getFileList();
for(File item : files){
Upload upload = new Upload(getActivity(), dropbox, "/Apps/Sink/", item, bar);
upload.executeOnExecutor(uploadExecutor);
}
}
else {
Upload upload = new Upload(getActivity(), dropbox, "/Apps/Sink/", file, bar);
upload.executeOnExecutor(uploadExecutor);
}
}
Upload 是一个实现 AsyncTask 并处理文件上传的类。
创建时,类的实例将条形设置为可见,在 OnPostExecute() 中将其设置为不可见
编辑
添加上传类。
public Upload(Context context, DropboxAPI<?> api, String dropboxPath,
File file, ProgressBar mBar) {
this.mBar = mBar;
mBar.setProgress(0);
mBar.setVisibility(View.VISIBLE)
@Override protected Boolean doInBackground(Void... params){
try {
// By creating a request, we get a handle to the putFile operation,
// so we can cancel it later if we want to
FileInputStream fis = new FileInputStream(mFile);
String path = mPath + mFile.getName();
mRequest = mApi.putFileOverwriteRequest(path, fis, mFile.length(),
new ProgressListener() {
@Override
public long progressInterval() {
// Update the progress bar every half-second or so
return 500;
}
@Override
public void onProgress(long bytes, long total) {
publishProgress(bytes);
}
});
if (mRequest != null) {
mRequest.upload();
return true;
}
@Override
protected void onProgressUpdate(Long... progress) {
int percent = (int)(100.0*(double)progress[0]/mFileLen + 0.5);
//mDialog.setProgress(percent);
mBar.setProgress(percent);
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
showToast("File successfully uploaded");
} else {
showToast(mErrorMsg);
}
mBar.setVisibility(View.INVISIBLE);
}
编辑
我通过将 for 循环移到上传类中解决了这个问题。 非常相似的方法HERE
【问题讨论】: