简体   繁体   English

通过Android Cursor的文件扩展名(或替代地,文件类型)筛选managedQuery

[英]Filter a managedQuery by file extension (or, alternatively, file type) for an Android Cursor

I want to do a managedQuery using Android SDK where the results returned are filtered by their respective file extension (eg not the name, necessarily). 我想使用Android SDK进行托管,其中返回的结果按其各自的文件扩展名进行过滤(例如,不一定是名称)。 I've done quite a bit of researching and am tired and hoping the community can help me.. I'm sure the answer is out there but, in lieu of reading an SQL book or whatever, I'm just trying to do something that should be pretty simple but not finding the solution. 我做了很多研究,很累,希望社区可以帮助我..我肯定答案就在那里,但是,除了阅读SQL书或其他什么,我只是想做点什么这应该很简单,但找不到解决方案。

What I want to do is, essentially, something like: 我想要做的事情基本上是这样的:

managedQuery(Audio.Media.EXTERNAL_CONTENT_URI, myProjection, Audio.Media.DATA + " like ? ", new String[] {"%mp3%"}, null );

This works EXCEPT that, if the title is something like "mymp3.wav", this query will still return it since 'mp3' is in the name but, that isn't what I want -> I want it filtered purely based on file extension (or, alternatively, file type if there is a way to do that [eg if an mp3's file extension name was changed to .wav even though the content of the file is actually mp3, that would work for my purposes as well]). 这工作除了,如果标题是“mymp3.wav”,这个查询仍将返回它,因为'mp3'在名称中,但这不是我想要的 - >我希望它纯粹基于文件过滤扩展(或者,如果有办法,可以使用文件类型[例如,如果mp3的文件扩展名更改为.wav,即使文件的内容实际上是mp3,也可​​以用于我的目的]) 。 The solution should indicate a way to filter for multiple extensions/file types (ie if I want the query to return all .mp3 files as well as .wav files but not .amr files, for example). 解决方案应指明一种过滤多种扩展/文件类型的方法(例如,如果我希望查询返回所有.mp3文件以及.wav文件,但不返回.amr文件)。

Furthermore, while I have you, I want to sort the results alphabetically. 此外,虽然我有你,但我想按字母顺序对结果进行排序。 I have tried something like: 我尝试过类似的东西:

managedQuery(Audio.Media.EXTERNAL_CONTENT_URI, myProjection, null, null, MediaStore.Audio.Media.TITLE + " ASC" );

However, that doesn't filter the entire results... that is to say that, the list returned is sorted by, say, mp3 title and then, at the end, it then appends to the list the .wav files also sorted alphabetically but I want the entire list to be alphabetical regardless of file type. 但是,这不会过滤整个结果...也就是说,返回的列表按照mp3标题进行排序,然后,最后,它会向列表追加.wav文件也按字母顺序排序但我希望整个列表都是按字母顺序排列的,无论文件类型如何。

I know someone on here can answer this easily -> thanks in advance!!! 我知道有人在这里可以轻松回答这个问题 - >提前谢谢!!!

I think all you have to do is change %mp3% to %mp3 . 我想你所要做的就是将%mp3%改为%mp3 This will force "mp3" to be at the end of the filename. 这将强制“mp3”位于文件名的末尾。

If you need to select multiple file types, do it like this: 如果您需要选择多种文件类型,请执行以下操作:

managedQuery(
  Audio.Media.EXTERNAL_CONTENT_URI,
  myProjection, 
  Audio.Media.DATA + " like ? OR " + Audio.Media.DATA + " like ? ", 
  new String[] {"%mp3","%wav"}, 
  MediaStore.Audio.Media.TITLE + " ASC" );

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

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