GDAA(Google Drive Android API):使用fileId

时间:2018-02-17 05:17:41

标签: android google-drive-api google-drive-android-api drive

我在我的应用中使用谷歌硬盘。我可以在Google drive App folder上传数据并获取文件ID。我的问题是,如何使用appDataFolderFileId下载 private List<String> getDataFromApi() throws IOException { List<String> fileInfo = new ArrayList<String>(); FileList lst = mService.files().list() .setSpaces("appDataFolder") .execute(); FileList result = mService.files().list() .setSpaces("appDataFolder") .setFields("nextPageToken, files(id, name, mimeType)") .execute(); List<File> files = result.getFiles(); if (files != null) { for (File file : files) { fileInfo.add(String.format("%s (%s) %s\n", file.getName(), file.getId(), file.getMimeType())); } } return fileInfo; } 这就是我获取文件ID的方式

compile('com.google.apis:google-api-services-drive:v3-rev102-1.23.0') {
    exclude group: 'org.apache.httpcomponents'
}

我正在使用Google驱动器服务的 v3

dotnet test

提前致谢

2 个答案:

答案 0 :(得分:0)

最后我得到了答案。希望这也有助于其他人从appDataFolder下载。在这里,我正在下载并将文件保存到SD card文件夹名Spectors。并传递com.google.api.services.api.drive.model.File的文件类型的值。

       private void downloadFile(boolean useDirectDownload, File uploadedFile)
            throws IOException {


        java.io.File direct = new java.io.File(Environment.getExternalStorageDirectory()
                + "/Spectors");
        if (!direct.exists()) {
            direct.mkdirs();
        }

        java.io.File parentDir = new java.io.File(direct.getAbsolutePath());
        if (!parentDir.exists() && !parentDir.mkdirs()) {
            throw new IOException("Unable to create parent directory");
        }
        OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getName()+".zip"));
        com.google.api.services.drive.Drive.Files.Get files = mService.files().get(uploadedFile.getId());
        files.executeMediaAndDownloadTo(out);

        Log.e(TAG, "downloadFile:2 " + files.toString() + "\n"+uploadedFile.getFileExtension() );
}

答案 1 :(得分:0)

您还可以采用以下方法从应用程序文件夹下载内容

通过以下代码从应用文件夹中获取文件

private void listFiles() {
    Query query =
            new Query.Builder()
                    .addFilter(Filters.or(Filters.eq(SearchableField.MIME_TYPE, "text/html"),
                            Filters.eq(SearchableField.MIME_TYPE, "text/plain")))
                    .build();
    getDriveResourceClient()
            .query(query)

            .addOnSuccessListener(this,

                    new OnSuccessListener<MetadataBuffer>() {
                        @Override
                        public void onSuccess(MetadataBuffer metadataBuffer) {
                            //mResultsAdapter.append(metadataBuffer);

                            for (int i = 0; i <metadataBuffer.getCount() ; i++) {
                                retrieveContents(metadataBuffer.get(i).getDriveId().asDriveFile());
                            }
                        }
                    }

            )
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e(TAG, "Error retrieving files", e);
                    MainActivity.this.finish();
                }
            });

}

使用以下代码下载内容

 private void retrieveContents(DriveFile file) {
        // [START drive_android_open_file]
        Task<DriveContents> openFileTask =
                getDriveResourceClient().openFile(file, DriveFile.MODE_READ_ONLY);
        // [END drive_android_open_file]
        // [START drive_android_read_contents]
        openFileTask
                .continueWithTask(task -> {
                    DriveContents contents = task.getResult();
                    // Process contents...
                    // [START_EXCLUDE]
                    // [START drive_android_read_as_string]
                    try (BufferedReader reader = new BufferedReader(
                                 new InputStreamReader(contents.getInputStream()))) {
                        StringBuilder builder = new StringBuilder();
                        String line;
                        while ((line = reader.readLine()) != null) {
                            builder.append(line).append("\n");
                        }
                        showMessage(getString(R.string.content_loaded));
                        mFileContents.setText(builder.toString());
                    }
                    // [END drive_android_read_as_string]
                    // [END_EXCLUDE]
                    // [START drive_android_discard_contents]
                    Task<Void> discardTask = getDriveResourceClient().discardContents(contents);
                    // [END drive_android_discard_contents]
                    return discardTask;
                })
                .addOnFailureListener(e -> {
                    // Handle failure
                    // [START_EXCLUDE]
                    Log.e(TAG, "Unable to read contents", e);
                    showMessage(getString(R.string.read_failed));
                    finish();
                    // [END_EXCLUDE]
                });
        // [END drive_android_read_contents]
    }