【问题标题】:Reading .xlsx Using Google Drive API使用 Google Drive API 读取 .xlsx
【发布时间】:2017-01-28 17:29:21
【问题描述】:

我正在尝试使用 Google Drive API 读取 .xlsx 文件,但是在我成功打开文件并决定读取内容后,它会显示垃圾值。我能够读取元数据并确认已打开正确的文件。这是成功读取文件后调用的内容。它适用于 .txt 文件,但我会收到任何 .word、.xlsx 文件的垃圾值。

private ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
            new ResultCallback<DriveApi.DriveContentsResult>() {
                @Override
                public void onResult(DriveApi.DriveContentsResult result) {
                    if (!result.getStatus().isSuccess()) {
                        Log.i(TAG, "Error while opening the file contents");
                        return;
                    }
                    Log.i(TAG, "File contents opened");
                    mProgressBar.setProgress(100);

                    DriveContents contents = result.getDriveContents();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String line;

                    try {
                        while ((line = reader.readLine()) != null) {
                            builder.append(line);
                        }
                    } catch (Exception e) {

                    }

                    String contentsAsString = builder.toString();

                    mTextViewContent.setText(contentsAsString);
                }
            };

【问题讨论】:

    标签: android google-drive-android-api


    【解决方案1】:

    使用 Google Drive API,downloading Google documents 是通过将您的文件导出为受支持的 MIME 类型来完成的。

    但是,如果您使用 Google Drive Android API,请在 opening the file contents

    为了能够读取文件,您必须首先以DriveFile.MODE_READ_ONLYDriveFile.MODE_READ_WRITE 模式打开其DriveContents 资源,具体取决于您更喜欢使用InputStream 还是ParcelFileDescriptor 类.

    您可能需要查看给定的文档以获取更详细的信息以及此GitHub post

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多