Android 使用 KitKat Storage Access Framework 后打开 Google Drive File Content URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25171246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open a Google Drive File Content URI after using KitKat Storage Access Framework
提问by Keith Entzeroth
I am using the Storage Access Framework for android 4.4 and opening the file picker.
我正在使用适用于 android 4.4 的存储访问框架并打开文件选择器。
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.
一切正常,除了从 Google Drive 选择文件时,我只能弄清楚如何将它作为输入流打开,但我想获得一个 java File 对象。
The content uri that's being returned looks something like this: content://com.google.android.apps.docs.storage/document/acc%3D4%3Bdoc%3D2279
返回的内容 uri 如下所示: 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:
其他类似但没有可行解决方案的问题可以让我获取文件名、文件大小和内容:
- Get real path from URI, Android KitKat new storage access framework
- Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT
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.
我还研究了 Paul Burke 的 FileChooser ( https://github.com/iPaulPro/aFileChooser),这是问题列表中最常见的问题。
How can I get a file from that content uri?
如何从该内容 uri 获取文件?
My current workaround is to write out a temporary file from an inputstream.
我目前的解决方法是从输入流中写出一个临时文件。
Thanks!
谢谢!
回答by Keith Entzeroth
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.
好的。我发现正确的方法是将来自其他帖子的输入流与来自 contentresolver 的一些数据结合使用。
For reference here are the hard to find android docs: https://developer.android.com/training/secure-file-sharing/retrieve-info.html
作为参考,这里很难找到 android 文档:https: //developer.android.com/training/secure-file-sharing/retrieve-info.html
The relevant code to get mimetype, filename, and filesize:
获取 mimetype、文件名和文件大小的相关代码:
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)));
And to get the file contents:
并获取文件内容:
getContentResolver().openInputStream(uri)
Hope this helps someone else.
希望这对其他人有帮助。
回答by Adeeb karim
this code solved my problem, when i tried to select image from google drive ,app get crashed ,
这段代码解决了我的问题,当我尝试从谷歌驱动器中选择图像时,应用程序崩溃了,
private void setImagePath(Intent data) throws Exception {
String wholeID="";
Uri selectedImage = data.getData();
if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.JELLY_BEAN_MR2){
wholeID=getUriPreKitkat(selectedImage);
}else {
wholeID = DocumentsContract.getDocumentId(selectedImage);
}
// Split at colon, use second item in the array
Log.i("debug","uri google drive "+wholeID);
String id = wholeID.split(":")[1];
String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getActivity().getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
}