![](/img/trans.png)
[英]Android Intent ACTION_GET_CONTENT does not return extension of file
[英]How to get the file name from an URI got from ACTION_GET_CONTENT Intent?
我使用ACTION_GET_CONTENT意图让用户从他的库中选择一首歌曲,然后将其作为URI在onActivityResult()
方法中返回。
我需要知道这首歌的名字。
我尝试了:
Uri song; //got from onActivityResult
String songName = song.getLastPathSegment();
与:
Uri song; //got from onActivityResult
File file = new File(song.getPath());
String songName = file.getName();
在这两种情况下, songName
字符串均如下所示:
// audio:9980
而我期望的是:
// Walk on the wild side.mp3
我究竟做错了什么?
我需要获取文件名。
它不是文件。 它是一个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
不是文件名。
我究竟做错了什么?
您假设ACTION_GET_CONTENT
给您一个文件。
欢迎您使用ContentResolver
对OpenableColumns
进行query()
并询问DISPLAY_NAME
。 我在此示例应用程序中对此进行了演示。 但是,请理解,您将获得一个显示名称,这取决于用户选择用来处理ACTION_GET_CONTENT
的应用程序,“显示名称”实际上是什么。 它可能是文件名,但不是必须的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.