【问题标题】:Can't get fileId of selected file from google drive in Drive rest Api无法从 Drive rest Api 中的谷歌驱动器获取所选文件的 fileId
【发布时间】:2019-03-14 06:27:58
【问题描述】:

现在 google drive api 已被弃用,所以我集成了他们的新 Drive REST API。在我的应用程序中,我想显示文件选择器以从谷歌驱动器中选择文档/图像。 我用过他们的样本: https://github.com/gsuitedevs/android-samples/blob/master/drive/deprecation/app/src/main/java/com/google/android/gms/drive/sample/driveapimigration/MainActivity.java

private void openFileFromFilePicker(Uri uri) {

    if (mDriveServiceHelper != null) {
        Log.d(TAG, "Opening " + uri.getPath());

        mDriveServiceHelper.openFileUsingStorageAccessFramework(getContentResolver(), uri)
                .addOnSuccessListener(nameAndContent -> {
                        String name = nameAndContent.first;
                        String content = nameAndContent.second;

                        mFileTitleEditText.setText(name);
                        mDocContentEditText.setText(content);

                        // Files opened through SAF cannot be modified.
                        setReadOnlyMode();
                    })
                .addOnFailureListener(exception ->
                        Log.e(TAG, "Unable to open file from picker.", exception));
    }
}

现在我想将所选文件上传到我的服务器上,因为我需要一个 fileID,但在 onActivityResult 我只能访问文件名和内容。所以请帮助我并建议我任何解决方案。我如何将选定的简历文件或图像从谷歌驱动器上传到我的服务器。

【问题讨论】:

    标签: android google-drive-android-api


    【解决方案1】:

    也许您已经找到问题的答案,但答案已经在您的问题中。您使用存储访问框架 (SAF)。打开文件时,可以将文件的内容下载到 UI。应该有一段额外的代码——这是加载内容的延续。

    /**
     * Opens the file at the {@code uri} returned by a Storage Access Framework {@link 
     Intent}
     * created by {@link #createFilePickerIntent()} using the given {@code contentResolver}.
     */
    public Task<Pair<String, String>> openFileUsingStorageAccessFramework(
            ContentResolver contentResolver, Uri uri) {
        return Tasks.call(mExecutor, () -> {
                // Retrieve the document's display name from its metadata.
                String name;
                try (Cursor cursor = contentResolver.query(uri, null, null, null, null)) {
                    if (cursor != null && cursor.moveToFirst()) {
                        Log.d(TAG, "cursor.getColumnCount(): " + cursor.getColumnCount());
                        for (int i = 0; i < cursor.getColumnCount(); i++) {
                            Log.d(TAG, i + " - " + cursor.getString(i) + "\n");
                        }
                        int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                        name = cursor.getString(nameIndex);
                    } else {
                        throw new IOException("Empty cursor returned for file.");
                    }
                }
    
                // Read the document's contents as a String.
                String content;
                try (InputStream is = contentResolver.openInputStream(uri);
                     BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
                    StringBuilder stringBuilder = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        stringBuilder.append(line);
                    }
                    content = stringBuilder.toString();
                }
    
                return Pair.create(name, content);
            });
    }
    

    查看注释下 // 将文档的内容作为字符串读取。

    关于 SAF: https://developer.android.com/guide/topics/providers/document-provider

    附: SAF 不提供对文件 ID 的访问以下载文件。 SAF 可以通过 uri 访问文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      相关资源
      最近更新 更多