我会尽量保持清醒。 我正在尝试从自定义图库中选择多个图像到To dropbox。从自定义库中选择图像时,它返回所选图像的路径数组。我将此数组转换为File []数组。然后将File []传递给Asynctask以将图像上载到dropbox。 问题是只有数组中的第一个图像被上传。
这是Upload.java文件的代码。
public class UploadFile extends AsyncTask<Void, Long, Boolean> {
private DropboxAPI<?> mApi;
private String mPath;
private File mFile;
private File[] fileMultipleUpload;
private long mFileLen;
private UploadRequest mRequest;
private Context mContext;
private ProgressDialog mDialog;
final static private String ACCOUNT_PREFS_NAME = "prefs";
private int flag;
private String mErrorMsg;
public UploadFile(Context context, DropboxAPI<?> api, String dropboxPath,
File[] file) {
flag = 2;
mContext = context;
mApi = api;
mPath = dropboxPath;
fileMultipleUpload = file;
mDialog = new ProgressDialog(context);
mDialog.setMax(fileMultipleUpload.length);
mDialog.setMessage("Uploading...");
mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDialog.setCancelable(false);
mDialog.setProgress(0);
mDialog.setButton("Cancel", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// This will cancel the putFile operation
mRequest.abort();
}
});
mDialog.show();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... params) {
try {
FileInputStream fis;
for (int j = 0; j < fileMultipleUpload.length; j++) {
File mfile = fileMultipleUpload[j];
publishProgress(Long.parseLong("" + j));
fis = new FileInputStream(mfile);
String path = mPath + mfile.getName();
mRequest = mApi.putFileOverwriteRequest(path, fis, mfile.length(), null);
mRequest.upload();
return true;
}
} catch (DropboxUnlinkedException e) {
// This session wasn't authenticated properly or user unlinked
mErrorMsg = "This app wasn't authenticated properly.";
} catch (DropboxFileSizeException e) {
// File size too big to upload via the API
mErrorMsg = "This file is too big to upload";
} catch (DropboxPartialFileException e) {
// We canceled the operation
mErrorMsg = "Upload canceled";
} catch (DropboxServerException e) {
// Server-side exception. These are examples of what could happen,
// but we don't do anything special with them here.
mErrorMsg = "1";
if (e.error == DropboxServerException._401_UNAUTHORIZED) {
// Unauthorized, so we should unlink them. You may want to
// automatically log the user out in this case.
mErrorMsg = "2";
} else if (e.error == DropboxServerException._403_FORBIDDEN) {
// Not allowed to access this
mErrorMsg = "3";
} else if (e.error == DropboxServerException._404_NOT_FOUND) {
// path not found (or if it was the thumbnail, can't be
// thumbnailed)
mErrorMsg = "4";
} else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
// user is over quota
mErrorMsg = "5";
} else {
// Something else
mErrorMsg = "6";
}
// This gets the Dropbox error, translated into the user's language
mErrorMsg = e.body.userError;
if (mErrorMsg == null) {
mErrorMsg = e.body.error;
}
} catch (DropboxIOException e) {
e.printStackTrace();
// Happens all the time, probably want to retry automatically.
mErrorMsg = "Network error. Try again.";
} catch (DropboxParseException e) {
// Probably due to Dropbox server restarting, should retry
mErrorMsg = "Dropbox error. Try again.";
} catch (DropboxException e) {
// Unknown error
mErrorMsg = "Unknown error. Try again.";
} catch (FileNotFoundException e) {
mErrorMsg = "7";
}
return false;
}
@Override
protected void onProgressUpdate(Long... progress) {
if (flag == 1) {
int percent = (int) (100.0 * (double) progress[0] / mFileLen + 0.5);
mDialog.setProgress(percent);
} else if (flag == 2) {
mDialog.setProgress(Integer.parseInt("" + progress[0]));
super.onProgressUpdate(progress);
}
}
@Override
protected void onPostExecute(Boolean result) {
mDialog.dismiss();
if (result) {
showToast("Successfully uploaded");
} else {
showToast(mErrorMsg);
}
}
我已检查fileMultipleUpload []中的值,并且它具有我要上传的文件的正确路径。 For循环中有什么问题吗?