我正在尝试将文件上传到我的GoogleDrive,但是我收到了IOException,我找不到原因?怎么了 ?应该修改什么? 凭据,文件路径,文件名,文件类型(text / csv)是正确的... parent_id folderID很好...... 显然我之前测试过如果连接到互联网...但没办法,我总是在线获得这个IOException:file = insert.execute()(我执行了一个调试器运行...)
private File uploadFile(boolean useDirectUpload) throws IOException {
java.io.File fileContent = new java.io.File(mFileUri.getPath());
com.google.api.services.drive.model.File fileMetadata = new File();
fileMetadata.setParents(Arrays.asList(new ParentReference().setId(mFolderId)));
fileMetadata.setTitle(fileContent.getName());
InputStreamContent mediaContent = new InputStreamContent("text/csv", new BufferedInputStream(new FileInputStream(fileContent)));
mediaContent.setLength(fileContent.length());
Drive.Files.Insert insert = mService.files().insert(fileMetadata, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(useDirectUpload);
uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);
uploader.setProgressListener(new FileUploadProgressListener());
File file = insert.execute();
//return insert.execute();
return file;
}
答案 0 :(得分:1)
parentID'mFolderID'为空..(这对任何id的消息都有意义..) 我修改了我的设计...以前使用2个线程和WeakReference ...一个用于获取folderID(并设置私有静态字符串mFolderId),另一个用于上传(使用mFolderID)...但是使用WeakReference,mFolderId设置为null为这个新线程
现在我只使用一个线程来获取folderID并在其中上传文件......
private void uploadFileInSwinglogFolder() {
mUploadFileInSwinglogFolderThread = new UploadFileInSwinglogFolderThread(UploadActivity.this);
mUploadFileInSwinglogFolderThread.start();
}
// static inner classes don't hold implicit references to their
// enclosing class, so the Activity instance won't be leaked across configuration change
private static class UploadFileInSwinglogFolderThread extends Thread {
private final WeakReference<UploadActivity> mActivity;
public UploadFileInSwinglogFolderThread(UploadActivity activity) {
mActivity = new WeakReference<UploadActivity>(activity);
}
private boolean mRunning = false;
@Override
public void run() {
UploadActivity activity = mActivity.get();
mRunning = true;
while (mRunning) {
mResultList = new ArrayList<File>();
mIOException = false;
mFolderId = null;
Files f1 = mService.files();
Files.List request = null;
try {
request = f1.list();
// searching for named folder at root level
String aQuery = "'root' in parents and mimeType='application/vnd.google-apps.folder' and title='"+ mFolderName + "'";
request.setQ(aQuery);
FileList fileList = request.execute();
mResultList.addAll(fileList.getItems());
request.setPageToken(fileList.getNextPageToken());
} catch (UserRecoverableAuthIOException e) {
activity.startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
Log.e(TAG, "STACKTRACE");
Log.e(TAG, Log.getStackTraceString(e));
mIOException = true;
Message msg = activity.mHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putInt("msgKey", UploadActivity.IO_EXCEPTION);
msg.setData(bundle);
activity.mHandler.sendMessage(msg);
} finally {
if (mResultList.size() == 0) {
Message msg = activity.mHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putInt("msgKey", UploadActivity.NO_SWIMLOGS_FOLDER);
msg.setData(bundle);
activity.mHandler.sendMessage(msg);
} else {
// Got the parent folderID in googleDrive
mFolderId = mResultList.get(0).getId();
File file = null;
try {
java.io.File fileContent = new java.io.File(mFileUri.getPath());
//FileContent mediaContent = new FileContent("text/csv", fileContent);
InputStreamContent mediaContent = new InputStreamContent("text/csv", new BufferedInputStream(new FileInputStream(fileContent)));
mediaContent.setLength(fileContent.length());
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/csv");
Log.d(TAG, "mFolderID: " + mFolderId);
body.setParents(Arrays.asList(new ParentReference().setId(mFolderId)));
Drive.Files.Insert insert = mService.files().insert(body, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);
// TODO设置进度监听器//uploader.setProgressListener(new FileUploadProgressListener());
file = insert.execute();
} catch (IOException e) {
Log.d(TAG, "IOException in uploading file code: ");
Log.e(TAG, "STACKTRACE");
Log.e(TAG, Log.getStackTraceString(e));
Message msg = activity.mHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putInt("msgKey", UploadActivity.IO_EXCEPTION);
msg.setData(bundle);
activity.mHandler.sendMessage(msg);
} finally {
Log.d(TAG, "end try - finally block ");
if ( file != null) {
Log.d(TAG, "Uploading file done: " + file.getTitle());
Log.d(TAG, "UploadActivity - thread - file uploaded");
Message msg = activity.mHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putInt("msgKey", UploadActivity.SWIMLOG_UPLOADED);
msg.setData(bundle);
activity.mHandler.sendMessage(msg);
}
}
}
mRunning = false;
}
}
}
public void close() {
mRunning = false;
}
}