如何获取谷歌驱动文件的文件名和真实路径? [英] How to get file name and real path of Google drive document?

查看:48
本文介绍了如何获取谷歌驱动文件的文件名和真实路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在文件管理器中获取文件的文件名和路径.但是,它不会返回 Google Drive 文件的路径.知道如何获得实际路径吗?

I am using the following code to get the file name and path of files in the File Manager. However it does not return a path for Google Drive files. Any idea how to obtain the actual path?

我的代码 -

public String getFilePath() {
    if (uri.getScheme().equalsIgnoreCase("file")) {
        return uri.getLastPathSegment();
    }

    cursorLoader.setUri(uri);
    cursorLoader.setProjection(projections);
    Cursor cursor = cursorLoader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
    cursor.moveToFirst();
    String realPath = cursor.getString(column_index);
    cursor.close();

    if (realPath == null || realPath.isEmpty()) {
        return null;
    }

return null;
}

推荐答案

你必须使用 URI.通过URI可以getContentResolver.query(theUriThatYouHave, null, null, null, null).现在有了光标,您可以检查列名等.

You have to use the URI. Through the URI you can getContentResolver.query(theUriThatYouHave, null, null, null, null). Now that you have a cursor, you can check column names etc.

对于 Google 驱动器,有一个列名称 _display_name.这将为您提供文件名.

For Google drive there is a column name _display_name. This will give you the file name.

现在您想访问该文件吗?您可以通过 getContentResolver().openInputStream(theUriThatYouHave) 打开 URI 的 InputStream.

Now you want access to the file? You can open an InputStream to the URI via getContentResolver().openInputStream(theUriThatYouHave).

这篇关于如何获取谷歌驱动文件的文件名和真实路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆