Daniele
Daniele

Reputation: 4343

How to get the file name from an URI got from ACTION_GET_CONTENT Intent?

I used a ACTION_GET_CONTENT Intent to let the user choose a song from his library, then it is returned in the onActivityResult() method as an URI.

I need to get the name of the song.

I tried with:

Uri song; //got from onActivityResult

String songName = song.getLastPathSegment();

and with:

Uri song; //got from onActivityResult

File file = new File(song.getPath());
String songName = file.getName();

in both cases the songNameString looks like this:

// audio:9980

while I'm expecting something like:

// Walk on the wild side.mp3

What am I doing wrong?

Upvotes: 0

Views: 548

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006584

I need to get the name of the file.

It is not a file. It is a Uri, just like https://stackoverflow.com/questions/43615264/how-to-get-the-file-name-from-an-uri-got-from-action-get-content-intent is a URI. how-to-get-the-file-name-from-an-uri-got-from-action-get-content-intent is not a filename.

What am I doing wrong?

You are assuming that ACTION_GET_CONTENT gives you a file.

You are welcome to use a ContentResolver to query() for the OpenableColumns and ask for the DISPLAY_NAME. I demonstrate that in this sample app. However, please understand that you are getting a display name, and it is up to the app that the user chooses to handle ACTION_GET_CONTENT what the "display name" actually is. It might be a filename, but it does not have to be.

Upvotes: 1

Related Questions