无法从Drive rest Api中的Google Drive获取所选文件的fileId

时间:2019-03-14 06:27:58

标签: android google-drive-android-api

现在不推荐使用Google Drive API,因此我已经集成了他们的新Drive REST API。在我的应用程序中,我想显示文件选择器以从Google Drive中选择文档/图像。 我用了他们的样品: 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中,我只能访问文件名和内容。因此,请帮助我,并向我提出任何解决方案。如何将选定的简历文件或图像从Google驱动器上传到服务器。

1 个答案:

答案 0 :(得分:0)

也许您已经找到问题的答案,但是答案已经在您的问题中了。您使用存储访问框架(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

P.S。 SAF不提供文件ID的访问权限来下载文件。 SAF可以通过uri访问文件。