假设我有code一款Android块看起来是这样的:
Suppose I have an Android block of code that looks something like this:
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);
唯一的问题,当然,是FILE_PATH实际上不是一个专栏中,我可以使用,而据我所知,不存在这样的列。
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.
所以,我想知道:
有没有办法在一个特定的目录查询只为音乐?如果是这样,怎么? 如果这不是一种选择,我应该做一个ListAdapter来过滤按目录?如果是这样,又怎么会在我这样做?去感谢您的任何建议。
推荐答案
确定,尝试多次迭代之后,我终于有工作的例子,我想我会分享它。我的例子查询图像MediaStore,即可获得缩略图每个图像在一个视图中显示。一些调整应为您提供查询音频MediaStore,而不是所需要的code。我加载我的图片到库对象,但不是这个code的工作要求:
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:
请确保你有一个光标和int在类级别定义的列索引,使得画廊的ImageAdapter有权访问它们:
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:
private Cursor cursor;
private int columnIndex;
首先,获得光标所在的文件夹中的图像标识的:
First, obtain a cursor of image IDs located in the folder:
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));
然后,在ImageAdapter的画廊,获取缩略图显示:
Then, in the ImageAdapter for the Gallery, obtain the thumbnail to display:
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;
}
我想这code中的最重要部分是,演示如何使用MediaStore查询过滤特定文件夹的图片文件列表中managedQuery。
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.
相关推荐
最新文章