如何获取文件名和谷歌驱动文件的真实路径? [英] How to get file name and real path of Google drive document?
问题描述
我用下面的code得到的文件管理器的文件名和文件的路径。然而,它不返回谷歌云端硬盘文件的路径。任何想法如何才能得到实际的路径?
我的code -
公共字符串getFilePath(){
如果(uri.getScheme()。equalsIgnoreCase(文件)){
返回uri.getLastPathSegment();
} cursorLoader.setUri(URI);
cursorLoader.setProjection(预测);
光标光标= cursorLoader.loadInBackground();
INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
cursor.moveToFirst();
字符串的realpath = cursor.getString(Column_Index中);
cursor.close(); 如果(真实路径== NULL || realPath.isEmpty()){
返回null;
}返回null;
}
您必须使用URI。通过URI可以 getContentResolver.query(theUriThatYouHave,NULL,NULL,NULL,NULL)
。现在,你有一个光标,可以查看列名等。
有关谷歌驱动器有一个列名 _display_name
。这会给你的文件名。
现在你想对文件的访问?您可以打开的InputStream
通过 getContentResolver openInputStream(theUriThatYouHave)的URI()
。
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?
My code -
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;
}
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.
For Google drive there is a column name _display_name
. This will give you the file name.
Now you want access to the file? You can open an InputStream
to the URI via getContentResolver().openInputStream(theUriThatYouHave)
.
这篇关于如何获取文件名和谷歌驱动文件的真实路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!