如何在Android上通过Google Drive REST API V3下载大文件(44MB)

时间:2018-02-24 02:43:32

标签: android google-drive-api

我正在努力让Android应用管理照片。此应用会创建一个zip文件并将其上传到Google云端硬盘进行备份。要恢复数据,请下载zip文件。但是,它无法下载文件。 zip文件的实际大小为44MB,但下载文件的大小为149bytes。如何通过Google Drive REST API下载大文件(44MB)?

zip文件的实际大小为44MB。 enter image description here

但是,根据Android Monitor,下载文件的大小为149字节。

CODE:

public class MainActivity extends AppCompatActivity {
    private static final String[] SCOPES = {DriveScopes.DRIVE};

    // same code as google drive rest api v3 quick start
    // (https://developers.google.com/drive/v3/web/quickstart/android)

    private class MakeRequestTask extends AsyncTask<Void, Void, List<String>> {
        private Drive service = null;
        private Exception lastError = null;

        MakeRequestTask(GoogleAccountCredential credential) {
            HttpTransport transport = AndroidHttp.newCompatibleTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
            service = new Drive.Builder(transport, jsonFactory, credential).setApplicationName("Weight loss tracker with pictures").build();
        }

        @Override
        protected List<String> doInBackground(Void... voids) {
            try {
                String pageToken = null;
                int count = 0;
                File target = null;
                do {
                    FileList result = service.files().list()
                            .setFields("nextPageToken, files(id, name, createdTime, size)")
                            .setPageToken(pageToken)
                            .execute();
                    for (File file : result.getFiles()) {
                        String fileName = file.getName();
                        if (fileName != null && !"".equals(fileName) && fileName.startsWith("backup-")) {
                            target = file;
                            System.out.println("size1:" + file.getSize());
                        }
                    }
                    pageToken = result.getNextPageToken();
                    System.out.println("PAGETOKEN:" + pageToken);
                } while (pageToken != null);

                if (target == null) {
                    System.out.println("There is no target to restore.");
                    return null;
                }

                String id = target.getId();
                System.out.println("DOWNLOAD TARGET ID:" + id);
                java.io.File appDataPath = getActivity().getApplicationContext().getFilesDir();
                String outputFileName = "restore.zip";
                java.io.File restoreFile = new java.io.File(appDataPath, outputFileName);
                try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                     FileOutputStream fos = new FileOutputStream(restoreFile)) {
                    service.files().get(id).executeAndDownloadTo(outputStream);
                    System.out.println("size2:" + outputStream.size());
                    outputStream.writeTo(fos);
                }

                System.out.println("size3:" + restoreFile.length());

                BackupUtil.getInstance().restore(restoreFile, appDataPath);

            } catch (UserRecoverableAuthIOException e) {
                e.printStackTrace();
                startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

Andriod Monitor:

I/System.out: add:{"createdTime":"2018-02-20T15:19:56.345Z","id":"1g5-R8vblk9Wp_NLCygP04z-Zu1kpjkNq","name":"backup-2018-02-21-00-19-35-976.zip","size":"46523824"}
I/System.out: size1:46523824
I/System.out: PAGETOKEN:null
I/System.out: DOWNLOAD TARGET ID:1g5-R8vblk9Wp_NLCygP04z-Zu1kpjkNq
I/System.out: size2:149
I/System.out: size3:149

修改

我添加了以下代码,以便知道149字节的文件数据。

String content = new String(outputStream.toByteArray());
System.out.println("content:" + content);

Android Monitor正在关注。

I/System.out: content:{
I/System.out:  "kind": "drive#file",
I/System.out:  "id": "1g5-R8vblk9Wp_NLCygP04z-Zu1kpjkNq",
I/System.out:  "name": "backup-2018-02-21-00-19-35-976.zip",
I/System.out:  "mimeType": "application/zip"
I/System.out: }

0 个答案:

没有答案