使用 KitKat 存储访问框架后打开 Google Drive File Content URI [英] Open a Google Drive File Content URI after using KitKat Storage Access Framework

查看:8
本文介绍了使用 KitKat 存储访问框架后打开 Google Drive File Content URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用适用于 android 4.4 的存储访问框架并打开文件选择器.

I am using the Storage Access Framework for android 4.4 and opening the file picker.

除了从 Google Drive 中选择文件外,一切正常,我只能弄清楚如何将其作为输入流打开,但我想获得一个 java File 对象.

Everything works except when choosing a file from Google Drive, I can only figure out how to open it as an input stream, but I would like to get a java File object.

返回的内容 uri 如下所示:content://com.google.android.apps.docs.storage/document/acc%3D4%3Bdoc%3D2279

The content uri that's being returned looks something like this: content://com.google.android.apps.docs.storage/document/acc%3D4%3Bdoc%3D2279

其他类似但没有可让我获取文件名、文件大小和内容的有效解决方案的问题:

Other questions that are similar but do not have a working solution which allows me to get the filename, filesize and contents:

我还研究了 Paul Burke 的 FileChooser ( https://github.com/iPaulPro/aFileChooser),这是问题列表中最常见的问题.

I've also looked into Paul Burke's FileChooser ( https://github.com/iPaulPro/aFileChooser) and this is the most common question on the issue's list.

如何从该内容 uri 中获取文件?

How can I get a file from that content uri?

我目前的解决方法是从输入流中写出一个临时文件.

My current workaround is to write out a temporary file from an inputstream.

谢谢!

推荐答案

好的.我发现正确的方法是将来自其他帖子的输入流与来自 contentresolver 的一些数据结合使用.

Ok. I found that the right way is to use the input stream from the other posts in conjunction with some data from the contentresolver.

这里有一些很难找到的 android 文档供参考:https://developer.android.com/training/secure-file-sharing/retrieve-info.html

For reference here are the hard to find android docs: https://developer.android.com/training/secure-file-sharing/retrieve-info.html

获取mimetype、文件名和文件大小的相关代码:

The relevant code to get mimetype, filename, and filesize:

Uri returnUri = returnIntent.getData();
String mimeType = getContentResolver().getType(returnUri);
Cursor returnCursor =
        getContentResolver().query(returnUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
TextView nameView = (TextView) findViewById(R.id.filename_text);
TextView sizeView = (TextView) findViewById(R.id.filesize_text);
nameView.setText(returnCursor.getString(nameIndex));
sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex)));

并获取文件内容:

getContentResolver().openInputStream(uri)

希望这对其他人有所帮助.

Hope this helps someone else.

这篇关于使用 KitKat 存储访问框架后打开 Google Drive File Content URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆