gpt4 book ai didi

java - 未触发上传进度监听器(Google Drive API)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:40:44 24 4
gpt4 key购买 nike

我想使用 Google Drive API 按上传文件的百分比显示进度。

我的menthod之前上传文件成功,但是看不到上传进度。

我已经看到并添加了 this FileUploadProgressListenermediaHttpUploader.getProgress() 仅显示 0.0(进度开始)和 1.0(进度完成时)。我无法及时获得进度百分比。

如何让它发挥作用?

这是我的上传代码:

public void UploadFile(final DFile uploadFile) {
if (!isLoggedIn()) {
OnUploadGoogleChecked(FALSE, "Not logged in");
return;
}
AsyncTask<Void, Long, String> task = new AsyncTask<Void, Long, String>() {
java.io.File fileContent;
FileContent mediaContent;
com.google.api.services.drive.model.File body;
com.google.api.services.drive.model.File file;
private ProgressDialog mDialog;
long mFileLen;

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mDialog = new ProgressDialog(act);
mDialog.setMax(100);
mDialog.setMessage("Uploading ");
mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDialog.setProgress(0);
mDialog.setButton("Cancel", new OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub

}
});
mDialog.show();
}

class FileUploadProgressListener implements MediaHttpUploaderProgressListener {

@Override
public void progressChanged(MediaHttpUploader uploader) throws IOException {
Log.d("Dolphin got percent", String.valueOf(uploader.getProgress()));
switch (uploader.getUploadState()) {
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
}

@Override
protected String doInBackground(Void... arg0) {
try {

java.io.File UPLOAD_FILE = new java.io.File(uploadFile.getNameAndDir());
// File's metadata.
fileContent = new java.io.File(uploadFile.getNameAndDir());
mFileLen = fileContent.length();

InputStreamContent mediaContent2 = new InputStreamContent("image/jpeg", new BufferedInputStream(new FileInputStream(UPLOAD_FILE)));
mediaContent2.setLength(UPLOAD_FILE.length());
body = new com.google.api.services.drive.model.File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
String parentId = null;
body.setParents(Arrays.asList(new ParentReference().setId(uploadFile.getFileHostFolderId())));


Drive.Files.Insert mInsert = service.files().insert(body, mediaContent2);
MediaHttpUploader uploader = mInsert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(true);
uploader.setProgressListener(new FileUploadProgressListener());
file = mInsert.execute();
if (file != null) {

}

} catch (UserRecoverableAuthIOException e) {

Log.d("Dolphin got error", "not login " + e);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


protected void onPostExecute(String result) {
mDialog.dismiss();
OnUploadGoogleChecked(TRUE, "Upload complete");
};
};
task.execute();
}

最佳答案

要取得进展,您必须使用 uploader.setDirectUploadEnabled(false);(而不是 true)和 uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE); 或任何必须是 MediaHttpUploader.MINIMUM_CHUNK_SIZE 的倍数的值,否则会导致错误。

关于java - 未触发上传进度监听器(Google Drive API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17986602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com