Android Can';无法在驱动器rest Api中从google驱动器获取所选文件的文件ID

Android Can';无法在驱动器rest Api中从google驱动器获取所选文件的文件ID,android,google-drive-android-api,Android,Google Drive Android Api,现在google drive api已被弃用,因此我集成了他们新的drive REST api。在我的应用程序中,我想显示文件选择器,以便从google drive中拾取文档/图像。 我使用了他们的样本: 私有void openFileFromFilePicker(Uri){ 现在我想在我的服务器上上传所选的文件,因为我需要一个文件ID,但在onActivityResult中,我只能访问文件名和内容。因此,请帮助我并向我建议任何解决方案。如何将所选的简历文件或图像从google drive上传

现在google drive api已被弃用,因此我集成了他们新的drive REST api。在我的应用程序中,我想显示文件选择器,以便从google drive中拾取文档/图像。 我使用了他们的样本:

私有void openFileFromFilePicker(Uri){


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

也许你已经找到了问题的答案,but答案已经在您的问题中。您使用的是存储访问框架(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);
        });
}
/**
*在存储访问框架{@链接返回的{@code uri}处打开文件
意图}
*由使用给定的{@code contentResolver}的{@link#createfilepickerinent()}创建。
*/
公共任务openFileUsingStorageAccessFramework(
ContentResolver ContentResolver,Uri){
返回任务。调用(mExecutor,()->{
//从文档的元数据中检索文档的显示名称。
字符串名;
try(Cursor Cursor=contentResolver.query(uri,null,null,null,null)){
if(cursor!=null&&cursor.moveToFirst()){
Log.d(标记“cursor.getColumnCount():”+cursor.getColumnCount());
对于(int i=0;i
在注释下查看//以字符串形式读取文档内容

关于SAF:

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

也许您已经找到问题的答案,但答案已经在您的问题中。您使用存储访问框架(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);
        });
}
/**
*在存储访问框架{@链接返回的{@code uri}处打开文件
意图}
*由使用给定的{@code contentResolver}的{@link#createfilepickerinent()}创建。
*/
公共任务openFileUsingStorageAccessFramework(
ContentResolver ContentResolver,Uri){
返回任务。调用(mExecutor,()->{
//从文档的元数据中检索文档的显示名称。
字符串名;
try(Cursor Cursor=contentResolver.query(uri,null,null,null,null)){
if(cursor!=null&&cursor.moveToFirst()){
Log.d(标记“cursor.getColumnCount():”+cursor.getColumnCount());
对于(int i=0;i
在注释下查看//以字符串形式读取文档内容

关于SAF:

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