Android从Google云端硬盘获取Uri路径

时间:2014-02-10 10:44:26

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

我有这个代码将文件上传到我的应用程序,当用文件管理器,dropbox或其他任何东西打开文件时,返回的路径是正确的,我可以访问它,我只遇到问题谷歌云端硬盘,它返回一些以“exposed_content”开头的路径,我不能以任何方式“解码”它,我搜索过并没有找到办法,任何人都有任何想法?

if (resultCode == Activity.RESULT_OK) {
            if ((data != null) && (data.getData() != null)) {
                final Uri filePath;
                if (data.getDataString().startsWith("content")) {
                    filePath = getRealPathFromURI(getApplicationContext(), data.getData());
                } else {
                    filePath = data.getData();
                }
                // TODO bug with google drive
                if (filePath.getLastPathSegment() != null) {
                    tvSelectedFile.setText("File selected: " + filePath.getLastPathSegment());

                } else {
                    tvSelectedFile.setText("File can not be accessed, please try another way");
                }

            }
}

1 个答案:

答案 0 :(得分:-1)

使用附加的代码...来自onActivity结果,您将获得内容uri ...将此uri传递给给定的方法......

public static String getGDriveDataColumn(Context context, Uri uri, String selection,
                                           String[] selectionArgs) {
    Cursor cursor = null;
    final String column = "_display_name";
    final String[] projection = {
        column
    };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
            return null;    

}