gpt4 book ai didi

java - 如何在我的 Android 应用程序中显示上传到 Google Drive 的进度?

转载 作者:行者123 更新时间:2023-12-02 06:32:18 28 4
gpt4 key购买 nike

我有以下代码

它应该将图像从我的 Android 设备上传到我的 Google 云端硬盘。

  private void saveFileToDrive() {

progressDialog = ProgressDialog.show(this, "", "Loading...");

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());

progressDialog.dismiss();
//startCameraIntent();
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}

问题是上传永远不会结束。这很奇怪,因为图像不太大

我想如果我通过原始云端硬盘应用程序上传它,速度会更快。

1) 如何显示上传进度?

2)上传完成后如何在同一个Activity中进行回调?

最佳答案

从 Google 云端硬盘下载文件:

  1. 您可以使用 AsyncTask,在其中可以从 url 获取输入流,并编写一个 while 循环来读取每一行并在对话框中显示下载的数据,如下所示。

    @Override
    protected Boolean doInBackground(FilesObject... files) {
    // getting an input Stream.
    HttpResponse resp = service.getRequestFactory()
    .buildGetRequest(new GenericUrl(file.SourceImageUrl)).execute();
    fileSize = resp.getHeaders().getContentLength();
    if (fileSize != -1) {
    fileSizeReadableString = DiskSpaceUtil.readableFileSize(fileSize);
    }
    InputStream is = resp.getContent();

    // publishing the progress in a dialog.
    int completed = 0;
    while ((length = inputStream.read(buffer)) > 0) {
    if (!this.isCancelled()) {
    completed += length;
    fOutputStream.write(buffer, 0, length);
    publishProgress(completed);
    } else {
    fOutputStream.flush();
    fOutputStream.close();
    inputStream.close();
    return;
    }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {

    sendShowloader("Downloading file ... \n"
    + DiskSpaceUtil.readableFileSize(number) + " of "
    + fileSizeReadableString);

    }
  2. 您可以使用固有的 Google Drive 的下载监听器,该监听器可以通过插入对象进行设置。

将文件上传到 Google 云端硬盘:

我们可以使用 Google 自己的 MediaHttpUploaderProgressListener 实现将文件上传到 Google Drive,如下所示:

    { // ... Inside some thread

java.io.File fil = new java.io.File(file.SourceImageUrl);
InputStream is = new FileInputStream(fil);

InputStreamContent mediaContent = new InputStreamContent(file.mimeType, is);
mediaContent.setLength(fil.length());

// File's metadata.
File body = new File();
body.setTitle(file.FileTitle);
body.setMimeType(file.mimeType);
body.setParents(parents);
body.setDescription("Content Uploaded to "+ DriveName);
// Inserting the Remote Resource into Google Drive
File destFile = null;

if(fil.length() > 5 * 1024 * 1024)
{
// Resumable Uploads when the file size exceeds a file size of 5 MB.
// check link :
// 1) https://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&r=08555cd2a27be66dc97505e15c60853f47d84b5a
// 2) http://stackoverflow.com/questions/15970423/uploading-downloading-of-large-size-file-to-google-drive-giving-error

Insert insert = mService.files().insert(body, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
uploader.setProgressListener(new FileUploadProgressListener());
destFile = insert.execute();

}
else
{
// Else we go by Non Resumable Uploads, which is safe for small uploads.
destFile = mService.files().insert(body, mediaContent).execute();
}

... }

// Now the implementation of FileUploadProgressListener which extends MediaHttpUploaderProgressListener

public static class FileUploadProgressListener implements MediaHttpUploaderProgressListener {

public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch (uploader.getUploadState()) {
case INITIATION_STARTED:
postToDialog("Initiation Started");
break;
case INITIATION_COMPLETE:
postToDialog("Initiation Completed");
break;
case MEDIA_IN_PROGRESS:
// postToDialog("Upload in progress");
postToDialog("Upload percentage: " + uploader.getProgress());
break;
case MEDIA_COMPLETE:
postToDialog("Upload Completed!");
break;

case NOT_STARTED :
postToDialog("Not Started!");
break;
}
}
}

关于java - 如何在我的 Android 应用程序中显示上传到 Google Drive 的进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19958252/

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