Google云端硬盘GET_CONTENT意图读取文件

时间:2013-07-27 11:19:31

标签: android google-drive-api file-get-contents

我在Android上有一个应用程序,可以使用像Dropbox这样的云存储来进行文件共享。为了开始分享,我抛出android.intent.action.SEND。 在显示的列表中,我看到了Google云端硬盘应用(之前已安装),因此我尝试将文件发送给它 - 它运行正常,文件显示在云端硬盘列表中。

然后,在另一台设备上我想读取这个文件。我扔了 android.intent.action.GET_CONTENT意图,选择云端硬盘然后不知道如何获取文件。我收到这样的Uri:

content://com.google.android.apps.docs.files/exposed_content/6jn9cnzdJbDywpza%2BlW3aA%3D%3D%0A%3BRV%2FaV94o%2FCcW4HGBYArtwOdHqt%2BrsYO4WmHcs6QWSVwp%2FXogkRAgit7prTnfp00a%0A

我不知道如何转换为物理文件路径。我怎么能从这里得到文件内容?

我在内容提供商处玩,可以获取文件名,但不是完整路径或其他任何内容。

对于Dropbox,我得到了file://样式的uri,笔直,简单,效果很好。

2 个答案:

答案 0 :(得分:5)

它向您发送内容提供商,您可以将其与ContentResolver一起使用,例如:

getContentResolver().query(Uri contentUri, String[] projection, String selection, String[] selectionArgs, String sortOrder);

编辑:要获取真实路径名,请使用下面提供的解决方案

Android: Getting a file URI from a content URI?

答案 1 :(得分:2)

我也遇到了同样的问题。我使用关注代码从Google云端硬盘文件中获取文件路径。它适用于SkyDrive和DropBox。

String filePath = null;
Uri _uri = data.getData();
Log.d("", "URI = " + _uri);                                       
if(_uri != null && "content".equals(_uri.getScheme()))  {
    Cursor cursor = this.getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Files.FileColumns.DATA }, null, null, null);
    cursor.moveToFirst();   
    filePath = cursor.getString(0);
    cursor.close();
} else {
     filePath = _uri.getPath();
}
Log.d("", "Chosen path = " + filePath);

我正在使用意图来选择文件。这是我选择文件的代码。

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
String strType = "*/*";
intent.setDataAndType(Uri.parse(dir.getAbsolutePath()), strType);
startActivityForResult(intent, PICKFILE_RESULT_CODE);

我的代码工作正常,当我从内部或外部存储器获取文件时。我希望使用相同的代码从Google云端硬盘获取文件。