17

我在 android 上有一个应用程序,它使用 Dropbox 等云存储进行文件共享。要开始分享,我会抛出android.intent.action.SEND. 在显示的列表中,我看到了 Google Drive 应用程序(以前安装),所以我尝试将文件发送给它 - 它工作正常,文件出现在 Drive 列表中。

然后,在另一台设备上我想读取这个文件。我抛出 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

我不知道如何转换为物理文件路径。我怎么能从中获取文件内容?

我在内容提供者周围玩了一下,可以获取文件名,但不能获取完整路径或其他任何内容。

对于保管箱,我得到了file://风格 uri,简单明了,效果很好。

4

2 回答 2

5

它正在向您发送内容提供者的 uri,您可以将其与 ContentResolver 一起使用,例如:

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

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

Android:从内容 URI 获取文件 URI?

于 2013-07-27T11:48:09.107 回答
2

我也面临同样的问题。我使用以下代码从 Google Drive 文件中获取文件路径。它适用于 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 Drive 获取文件。

于 2013-12-09T13:11:00.233 回答