在Android中,我如何查询MediaStore中特定路径下的文件?或者只显示特定路径下的文件?

5
假设我有一个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);

但我想要的是这个:
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实际上不是我可以使用的列,据我所知,没有这样的列存在。
所以我在想:
  1. 有没有一种方法只查询某个目录中的音乐?如果有,应该如何操作?
  2. 如果不行,是否应该制作一个过滤器,通过目录筛选ListAdapter?如果是,那么如何操作?
感谢任何帮助。
2个回答

13

好的,在多次尝试后,我终于有一个可行的例子,并且想要分享一下。我的示例查询了图片MediaStore,然后获取每个图片的缩略图以在视图中显示。稍作调整即可为您提供所需的代码以查询音频MediaStore。我将我的图片加载到Gallery对象中,但这不是此代码工作所必需的:

确保在类级别上定义了一个游标和列索引的int,以便Gallery的ImageAdapter可以访问它们:

private Cursor cursor;
private int columnIndex;

首先,获取位于文件夹中的图像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));

接下来,在Gallery的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;
}

我猜这段代码最重要的部分是managedQuery,它演示了如何使用MediaStore查询来过滤特定文件夹中的图像文件列表。


-1

MediaStore.MediaColumns.DATA 是你要找的类似于“文件路径”的东西。

因此,可以通过以下方式检索文件路径:

String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接