使用奇巧存储访问架构后,打开谷歌驱动器文件内容的URI [英] Open a Google Drive File Content URI after using KitKat Storage Access Framework
问题描述
我现在用的存储访问架构的Android 4.4系统,并打开文件选择器。
I am using the Storage Access Framework for android 4.4 and opening the file picker.
一切正常选择从谷歌驱动器文件时除外,我只能找出如何打开它作为一个输入流,但我想获得一个java文件对象。
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看起来是这样的:内容://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:
- <一个href="http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework">Get从URI真实路径,Android的奇巧新的存储访问架构
- <一个href="http://stackoverflow.com/questions/19834842/android-gallery-on-kitkat-returns-different-uri-for-intent-action-get-content">Android在奇巧画廊返回不同的URI Intent.ACTION_GET_CONTENT
- Get real path from URI, Android KitKat new storage access framework
- Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT
我也看着保罗·伯克的文件选择器( 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?
我目前的解决方法是从一个InputStream写出一个临时文件。
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的文档:<一href="https://developer.android.com/training/secure-file-sharing/retrieve-info.html">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
相关code得到的mimetype,文件名,文件大小和:
The relevant code to get mimetype, filename, and filesize:
Uri returnUri = returnIntent.getData();
String mimeType = getContentResolver().getType(returnUri);
Uri returnUri = returnIntent.getData();
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.
这篇关于使用奇巧存储访问架构后,打开谷歌驱动器文件内容的URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!