我正在尝试将文件上传到保管箱,我还创建了一个进度条来检查其状态,但在执行时会引发异常。我创建了一个 UploadFile 类的对象并调用 UploadFile 函数,传递的参数是 UploadData.this, mApi,FILE_DIR,file。
public class UploadFile extends AsyncTask<Void, Long, Boolean> {
private DropboxAPI<?> mApi;
private String mPath;
private File mFile;
private long mFileLen;
private UploadRequest mRequest;
private Context mContext;
private final ProgressDialog mDialog;
private String mErrorMsg;
public UploadFile(Context context, DropboxAPI<?> api, String dropboxPath,
File file)
{
mContext = context.getApplicationContext();
mFileLen = file.length();
mApi = api;
mPath = dropboxPath;
mFile = file;
mDialog = new ProgressDialog(context);
mDialog.setMax(100);
mDialog.setMessage("Uploading " + file.getName());
mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
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 Boolean doInBackground(Void... params) {
try {
FileInputStream fis = new FileInputStream(mFile);
String path = mPath + mFile.getName();
mRequest = mApi.putFileOverwriteRequest(path, fis, mFile.length(),
new ProgressListener() {
@Override
public long progressInterval() {
return 500;
}
public void onProgress(long bytes, long total) {
publishProgress(bytes);
}
});
if (mRequest != null) {
mRequest.upload();
return true;
}
} catch (DropboxUnlinkedException e) {
mErrorMsg = "This app wasn't authenticated properly.";
} catch (DropboxFileSizeException e) {
mErrorMsg = "This file is too big to upload";
} catch (DropboxPartialFileException e) {
mErrorMsg = "Upload canceled";
} catch (DropboxServerException e) {
if (e.error == DropboxServerException._401_UNAUTHORIZED) {
} else if (e.error == DropboxServerException._403_FORBIDDEN) {
} else if (e.error == DropboxServerException._404_NOT_FOUND) {
} else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
// user is over quota
} else {
// Something else
}
// 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) {
mErrorMsg = "Network error. Try again.";
} catch (DropboxParseException e) {
mErrorMsg = "Dropbox error. Try again.";
} catch (DropboxException e) {
mErrorMsg = "Unknown error. Try again.";
} catch (FileNotFoundException e) {
}
return false;
}
@Override
protected void onProgressUpdate(Long... progress) {
int percent = (int)(100.0*(double)progress[0]/mFileLen + 0.5);
mDialog.setProgress(percent);
}
@Override
protected void onPostExecute(Boolean result) {
mDialog.dismiss();
if (result) {
showToast("Image successfully uploaded");
} else {
showToast(mErrorMsg);
}
}
private void showToast(String msg) {
Toast error = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
error.show();
}
}
这是日志(我是新手,我不正确理解日志)
06-30 06:18:34.400: W/dalvikvm(422): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
06-30 06:18:34.410: E/AndroidRuntime(422): FATAL EXCEPTION: AsyncTask #1
06-30 06:18:34.410: E/AndroidRuntime(422): java.lang.RuntimeException: An error occured while executing doInBackground()
06-30 06:18:34.410: E/AndroidRuntime(422): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-30 06:18:34.410: E/AndroidRuntime(422): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-30 06:18:34.410: E/AndroidRuntime(422): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-30 06:18:34.410: E/AndroidRuntime(422): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-30 06:18:34.410: E/AndroidRuntime(422): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-30 06:18:34.410: E/AndroidRuntime(422): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-30 06:18:34.410: E/AndroidRuntime(422): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-30 06:18:34.410: E/AndroidRuntime(422): at java.lang.Thread.run(Thread.java:1096)
06-30 06:18:34.410: E/AndroidRuntime(422): Caused by: java.lang.NullPointerException
06-30 06:18:34.410: E/AndroidRuntime(422): at com.sachin.UploadFile.doInBackground(UploadFile.java:59)
06-30 06:18:34.410: E/AndroidRuntime(422): at com.sachin.UploadFile.doInBackground(UploadFile.java:1)
06-30 06:18:34.410: E/AndroidRuntime(422): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-30 06:18:34.410: E/AndroidRuntime(422): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)