繁体   English   中英

如何从ACTION_GET_CONTENT Intent获取的URI中获取文件名?

[英]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给您一个文件。

欢迎您使用ContentResolverOpenableColumns进行query()并询问DISPLAY_NAME 我在此示例应用程序对此进行了演示。 但是,请理解,您将获得一个显示名称,这取决于用户选择用来处理ACTION_GET_CONTENT的应用程序,“显示名称”实际上是什么。 可能是文件名,但不是必须的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM