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;
}
Uri
. The code snippet that you are using will only work -- at best -- with files indexed byMediaStore
. Applications like Google Drive are welcome to make file content available viaACTION_GET_CONTENT
,ACTION_PICK
, the storage access framework, etc. However, those files do not have to be anywhere that you can access them independently, such as having them on internal storage.ContentProvider
could be decrypting an encrypted file on the fly and streaming that back to clients. TheContentProvider
could be streaming it off of the Internet. And so on. There has never been a requirement that aUri
map to a file, which is why yourMediaStore
hack is unreliable for all versions of Android. FewerUri
values will map to files on Android 4.4+ with the storage access framework.