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

Sar*_*mer 8 android android-file google-drive-api

我有这个代码将文件上传到我的应用程序,当用文件管理器,dropbox或其他任何东西打开文件时,返回的路径是正确的,我可以访问它,我只是遇到Google Drive问题,它返回一些以"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");
                }

            }
}
Run Code Online (Sandbox Code Playgroud)

Byt*_*ode -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;    

}
Run Code Online (Sandbox Code Playgroud)