How to obtain the file path from a Google Drive URI in an Android application

I’m developing an Android application that allows users to select files from various sources. I’ve created a function called retrieveFilePath that is supposed to convert URIs into actual file paths. This function works great for files from the gallery and those downloaded, but I am facing an issue when attempting to get the path for files selected from Google Drive.

Here’s a sample Google Drive URI I’m working with: content://com.google.android.apps.docs.storage/document/acc%3D25%3Bdoc%3D12

Can someone guide me on how to address this specific problem? Below is my current code implementation:

public static String retrieveFilePath(final Context context, final Uri uri) {
    final boolean isRecentAndroidVersion = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    
    if (isRecentAndroidVersion && DocumentsContract.isDocumentUri(context, uri)) {
        if (isStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] splitDocs = docId.split(":");
            final String type = splitDocs[0];
            
            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + splitDocs[1];
            }
        }
        else if (isDownloadDocument(uri)) {
            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));
            return getDataColumn(context, contentUri, null, null);
        }
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] splitDocs = docId.split(":");
            final String type = splitDocs[0];
            
            Uri mediaContentUri = null;
            if ("image".equals(type)) {
                mediaContentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                mediaContentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } 
            
            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { splitDocs[1] };
            
            return getDataColumn(context, mediaContentUri, selection, selectionArgs);
        }
        else if (isGoogleDriveUri(uri)) {
            // Implement logic here for Google Drive file path retrieval
        }
    }
    else if ("content".equalsIgnoreCase(uri.getScheme())) {
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();
        return getDataColumn(context, uri, null, null);
    }
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    
    return noPathFound;
}

public static String readInputStream(InputStream inputStream) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream), 2048);
    String line;
    StringBuilder result = new StringBuilder();
    try {
        while ((line = reader.readLine()) != null) {
            result.append(line);
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result.toString();
}

Google Drive URIs aren’t real file paths - they’re just references to cloud files. When someone picks a file from Google Drive, Android gives you a content URI, not an actual location on the device. Don’t try extracting a file path from these. Instead, download the content directly. Use ContentResolver to grab the file data through the URI. Here’s my usual approach: check if it’s a Google Drive URI, then open an InputStream with contentResolver.openInputStream(uri). You can either work with the stream directly or dump it into your app’s internal storage for a temp file. This method’s been solid for me. Google Drive and other cloud providers won’t give you direct file paths because the files aren’t stored locally - that’s by design. Your code structure looks fine, just handle the Google Drive case differently than local storage.

Your problem is that Google Drive files don’t have file paths like local files do. They’re stored in the cloud, not on your device, so trying to get a file path will always fail.

Ditch the file path approach completely. Work directly with the URI using ContentResolver instead. Open an InputStream from the URI and either process the data directly or copy it to a temp file if you really need a path. I’ve hit this same issue in multiple projects - this is the only way that works.

Use context.getContentResolver().openInputStream(uri) to read the file. If your code absolutely needs a file path, create a temp file in your app’s cache directory and copy the stream there. This works consistently across Android versions and cloud storage providers.

you cant get a real file path for google drive files since theyre in the cloud. try using contentResolver.openInputStream() to work with the file directly or copy it to local storage for easier access. its the only way to go with this!