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

标签 java android

假设我有一个 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?如果是这样,我将如何着手去做?

感谢您的任何建议。

最佳答案

好的,经过多次反复尝试,我终于有了一个可行的示例,我想我会分享它。我的示例查询图像 MediaStore,然后获取每个图像的缩略图以显示在 View 中。一些调整应该会为您提供查询音频 MediaStore 所需的代码。我正在将我的图像加载到 Gallery 对象中,但这不是此代码工作的必要条件:

确保在类级别为列索引定义了 Cursor 和 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 查询来过滤特定文件夹中的图像文件列表。

关于java - 在 Android 中,如何仅查询 MediaStore 中特定路径中的文件?或者,只显示特定路径中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6208315/

相关文章:

java - 错误 : Java:31 cannot find symbol

java - Android 网络服务 PHP MYSQL

android - 在 Android 中为通用文件选择哪种 mimeType?

android - 我可以让工具栏有两行并仍然使用菜单项吗?

java - 在我的 quartz 工作中注入(inject)

java - JSON反序列化问题-解析Instant时出错

java - 我想在 DoubleClick 事件上显示一些文本该怎么做?我已经附上了我的源代码

java - java 是否支持 if-then-else 正则表达式构造(Perl 构造)?

android - 在自定义 Runnable 中使用 Retrofit 执行请求

java - 如何强制 Proguard 保留我的 .xml 资源文件?