gpt4 book ai didi

java - 检查上传和下载的进度(适用于 Android 或 Java 的 Google Drive API)

转载 作者:太空狗 更新时间:2023-10-29 22:57:26 29 4
gpt4 key购买 nike

如何检查使用 GoogleDrive API 的上传进度?

service.files().insert(body, mediaContent).execute(); 仅返回一个文件/strong> 我们可以检查文件是否已完全上传。

但我需要实时检查进度(至少每秒左右),无论如何都要这样做?

对于下载部分,我认为我们可以手动比较我们已经从输入流中读取的字节数与总文件大小,然后计算当前进度。

但这是正确的方法吗?或者还有其他更好的检查进度的方法吗?

private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
// File's binary content
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);

// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");

File file = service.files().insert(body, mediaContent).execute();
if (file != null) {
showToast("Photo uploaded: " + file.getTitle());
//startCameraIntent();
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}

已编辑:使用此代码 http://code.google.com/p/google-api-java-client/source/browse/drive-cmdline-sample/src/main/java/com/google/api/services/samples/drive/cmdline/DriveSample.java?repo=samples&name=based-on-1.12&r=08555cd2a27be66dc97505e15c60853f47d84b5a

如果我使用可恢复上传,我现在可以取得一些进展,我将 block 大小设置为 1MB。但是现在有两个问题。

1)断点续传比单次上传慢很多,因为中间断了很多次,每次只发送1MB。
如果我将 chunksize 设置得更高,那么它根本不会显示较小文件的进度。
所以,断点续传的进展并不是我真正想要的。我想要单次上传的进度。

2) 如果我将 block 大小设置为大约 1MB,对于一个普通的 10MB 文件,我的上传总是失败大约 70%。
如果我将 chunksize 设置为 10MB,那么它将成功,但我看不到任何进展。

Drive.Files.Insert insert = service.files().insert(body, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
uploader.setChunkSize(10*1024*1024); // previously I am using 1000000 thats why it won't work
uploader.setProgressListener(new FileUploadProgressListener());
com.google.api.services.drive.model.File f = insert.execute();

LogCat 错误:

11-27 19:23:26.927: D/FileUploadProgressListener(11527): Upload is In Progress: 0.5235538030501893
11-27 19:23:33.692: D/FileUploadProgressListener(11527): Upload is In Progress: 0.56095050326806
11-27 19:23:38.294: D/FileUploadProgressListener(11527): Upload is In Progress: 0.5983472034859306
11-27 19:23:50.583: D/FileUploadProgressListener(11527): Upload is In Progress: 0.6357439037038013
11-27 19:23:55.091: D/FileUploadProgressListener(11527): Upload is In Progress: 0.673140603921672
11-27 19:24:05.130: D/FileUploadProgressListener(11527): Upload is In Progress: 0.7105373041395426
11-27 19:24:17.005: D/FileUploadProgressListener(11527): Upload is In Progress: 0.7479340043574133
11-27 19:24:28.888: D/FileUploadProgressListener(11527): Upload is In Progress: 0.785330704575284
11-27 19:24:28.896: W/HttpTransport(11527): exception thrown while executing request
11-27 19:24:28.896: W/HttpTransport(11527): java.io.IOException: unexpected end of stream
11-27 19:24:28.896: W/HttpTransport(11527): at libcore.net.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:58)
11-27 19:24:28.896: W/HttpTransport(11527): at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:88)
11-27 19:24:28.896: W/HttpTransport(11527): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:980)
11-27 19:24:28.896: W/HttpTransport(11527): at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:293)
11-27 19:24:28.896: W/HttpTransport(11527): at com.google.api.services.drive.Drive$Files$Insert.executeUnparsed(Drive.java:309)
11-27 19:24:28.896: W/HttpTransport(11527): at com.google.api.services.drive.Drive$Files$Insert.execute(Drive.java:331)

最佳答案

进度监听器:

private class FileUploadProgressListener implements MediaHttpUploaderProgressListener {

private String mFileUploadedName;

public FileUploadProgressListener(String fileName) {
mFileUploadedName = fileName;
}

@Override
public void progressChanged(MediaHttpUploader mediaHttpUploader) throws IOException {
if (mediaHttpUploader == null) return;
switch (mediaHttpUploader.getUploadState()) {
case INITIATION_STARTED:
//System.out.println("Initiation has started!");
break;
case INITIATION_COMPLETE:
//System.out.println("Initiation is complete!");
break;
case MEDIA_IN_PROGRESS:
double percent = mediaHttpUploader.getProgress() * 100;
if (Ln.DEBUG) {
Log.d(Ln.TAG, "Upload to Dropbox: " + mFileUploadedName + " - " + String.valueOf(percent) + "%");
}
notif.setProgress(percent, mFileUploadedName).fire();
break;
case MEDIA_COMPLETE:
//System.out.println("Upload is complete!");
}
}
}

更多信息在这里: https://code.google.com/p/google-api-java-client/wiki/MediaUpload

Direct media upload will upload the whole media content in one request as opposed to the resumable media upload protocol that could upload in multiple requests.

Doing a direct upload reduces the number of HTTP requests but increases the change of failures with a large media upload (e.g. connection failure).

由于库的体系结构,您必须在分 block 和进度之间做出选择,或者在没有进度的情况下选择一步。最小的 block 大小应该会有所改善。可能更小的 block :

setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE)

希望对您有所帮助!

关于java - 检查上传和下载的进度(适用于 Android 或 Java 的 Google Drive API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13580109/

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