java 在 Android 中,如何仅查询 MediaStore 特定路径中的文件?或者,只显示特定路径中的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6208315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Android, how do I query MediaStore only for files in a specific path? Or alternatively, only display files in a certain path?
提问by Ken Kinder
Suppose I have an Android block of code that looks something like this:
假设我有一个如下所示的 Android 代码块:
String[] proj = {MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media._ID};
int[] to = new int[] { R.id.artist_name };
Cursor musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, MediaStore.Audio.Media.ARTIST);
ListView musiclist = (ListView) findViewById(R.id.mylist);
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.songitem, musiccursor, proj, to);
musiclist.setAdapter(mAdapter);
But what I want, is this:
但我想要的是:
String selection = MediaStore.Audio.Media.FILE_PATH + " ilike '%audio%books%'";
String[] proj = {MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media._ID};
int[] to = new int[] { R.id.artist_name };
Cursor musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, null, MediaStore.Audio.Media.ARTIST);
ListView musiclist = (ListView) findViewById(R.id.mylist);
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.songitem, musiccursor, proj, to);
musiclist.setAdapter(mAdapter);
The only problem, of course, is that FILE_PATH is not actually a column I can use, and as far as I can tell, no such column exists.
当然,唯一的问题是 FILE_PATH 实际上不是我可以使用的列,据我所知,不存在这样的列。
So I'm wondering:
所以我想知道:
- Is there a way to query only for music in a certain directory? If so, how?
- If that's not an option, should I make a ListAdapter that filters by directory? If so, again, how would I go about in doing that?
- 有没有办法只查询某个目录中的音乐?如果是这样,如何?
- 如果这不是一个选项,我应该制作一个按目录过滤的 ListAdapter 吗?如果是这样,我将如何去做呢?
Thanks for any advice.
感谢您的任何建议。
回答by achildress
OK, after many iterations of trying, I finally have an example that works and I thought I'd share it. My example queries the images MediaStore, then obtains the thumbnail for each image to display in a view. A few tweaks should provide you with the code needed to query the audio MediaStore instead. I am loading my images into a Gallery object, but that is not a requirement for this code to work:
好的,经过多次尝试,我终于有了一个有效的例子,我想我会分享它。我的示例查询图像 MediaStore,然后获取要在视图中显示的每个图像的缩略图。一些调整应该会为您提供查询音频 MediaStore 所需的代码。我正在将我的图像加载到 Gallery 对象中,但这不是此代码工作的要求:
Make sure you have a Cursor and int for the column index defined at the class level so that the Gallery's ImageAdapter has access to them:
确保您有一个 Cursor 和 int 用于在类级别定义的列索引,以便 Gallery 的 ImageAdapter 可以访问它们:
private Cursor cursor;
private int columnIndex;
First, obtain a cursor of image IDs located in the folder:
首先,获取位于文件夹中的图像 ID 的光标:
Gallery g = (Gallery) findViewById(R.id.gallery);
// request only the image ID to be returned
String[] projection = {MediaStore.Images.Media._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%myimagesfolder%"},
null);
// Get the column index of the image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
g.setAdapter(new ImageAdapter(this));
Then, in the ImageAdapter for the Gallery, obtain the thumbnail to display:
然后,在图库的 ImageAdapter 中,获取要显示的缩略图:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// obtain the image URI
Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
String url = uri.toString();
// Set the content of the image based on the image URI
int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
i.setImageBitmap(b);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
I guess the most important section of this code is the managedQuery that demonstrates how to use MediaStore queries to filter a list of image files in a specific folder.
我想这段代码最重要的部分是 managedQuery ,它演示了如何使用 MediaStore 查询来过滤特定文件夹中的图像文件列表。
回答by null param
MediaStore.MediaColumns.DATAis what you are looking for something like 'FILE_PATH'.
MediaStore.MediaColumns.DATA是您正在寻找的类似“FILE_PATH”的东西。
So file path can be retrieved by
所以文件路径可以通过
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));