我正在使用以下代码获取文件管理器中文件的文件名和路径。但是,它不会返回Google Drive文件的路径。有什么办法可以获取实际路径吗?
我的代码 -
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
都没有必要有一个“实际路径”。您正在使用的代码片段最多只能与由MediaStore
索引的文件一起使用。像Google Drive这样的应用程序可以通过ACTION_GET_CONTENT
,ACTION_PICK
,存储访问框架等方式提供文件内容。但是,这些文件不必在您可以独立访问它们的任何地方,例如将它们放在内部存储中。 - CommonsWare