[英]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. 我使用ACTION_GET_CONTENT意图让用户从他的库中选择一首歌曲,然后将其作为URI在onActivityResult()
方法中返回。
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 songName
String looks like this: 在这两种情况下, songName
字符串均如下所示:
// audio:9980
while I'm expecting something like: 而我期望的是:
// Walk on the wild side.mp3
What am I doing wrong? 我究竟做错了什么?
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. 它是一个Uri
,就像https://stackoverflow.com/questions/43615264/how-to-get-the-file-name-from-an-uri-got-from-action-get-content-intent
是URI。 how-to-get-the-file-name-from-an-uri-got-from-action-get-content-intent
is not a filename. how-to-get-the-file-name-from-an-uri-got-from-action-get-content-intent
不是文件名。
What am I doing wrong? 我究竟做错了什么?
You are assuming that ACTION_GET_CONTENT
gives you a file. 您假设ACTION_GET_CONTENT
给您一个文件。
You are welcome to use a ContentResolver
to query()
for the OpenableColumns
and ask for the DISPLAY_NAME
. 欢迎您使用ContentResolver
对OpenableColumns
进行query()
并询问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. 但是,请理解,您将获得一个显示名称,这取决于用户选择用来处理ACTION_GET_CONTENT
的应用程序,“显示名称”实际上是什么。 It might be a filename, but it does not have to be. 它可能是文件名,但不是必须的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.