How to extract the file name from URI returned from Intent.ACTION_GET_CONTENT?
<p>I am using 3rd party file manager to pick a file (PDF in my case) from the file system. </p>

<p><strong>This is how I launch the activity:</strong></p>

<pre><code>Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(getString(R.string.app_pdf_mime_type));
intent.addCategory(Intent.CATEGORY_OPENABLE);

String chooserName = getString(R.string.Browse);
Intent chooser = Intent.createChooser(intent, chooserName);

startActivityForResult(chooser, ActivityRequests.BROWSE);
</code></pre>

<p><strong>This is what I have in <code>onActivityResult</code>:</strong></p>

<pre><code>Uri uri = data.getData();
if (uri != null) {
    if (uri.toString().startsWith("file:")) {
        fileName = uri.getPath();
    } else { // uri.startsWith("content:")

        Cursor c = getContentResolver().query(uri, null, null, null, null);

        if (c != null &amp;&amp; c.moveToFirst()) {

            int id = c.getColumnIndex(Images.Media.DATA);
            if (id != -1) {
                fileName = c.getString(id);
            }
        }
    }
}
</code></pre>

<p>Code snippet is borrowed from <em>Open Intents File Manager</em> instructions available here:<br />
<a href="http://www.openintents.org/en/node/829" rel="nofollow">http://www.openintents.org/en/node/829</a></p>

<p>The purpose of <code>if-else</code> is backwards compatibility. I wonder if this is a best way to get the file name as I have found that other file managers return all kind of things.<br /> 
<br />
For example, <em>Documents ToGo</em> return something like the following: <br /></p>

<pre><code>content://com.dataviz.dxtg.documentprovider/document/file%3A%2F%2F%2Fsdcard%2Fdropbox%2FTransfer%2Fconsent.pdf
</code></pre>

<p>on which <code>getContentResolver().query()</code> returns <code>null</code>.<br />
<br />
To make things more interesting, unnamed file manager (I got this URI from client log) returned something like:</p>

<pre><code>/./sdcard/downloads/.bin
</code></pre>

<p><br />
<strong>Is there a preferred way of extracting the file name from URI or one should resort to string parsing?</strong></p>
