Android URI到Cursor

时间:2014-01-23 08:00:38

标签: android

如果我将sdcard中的文件路径的URI转换为android

中的Cursor
Cursor cursor = getContentResolver().query(XXX, null, null, null, null);
if(null != cursor)
{
    cursor.moveToFirst();
    mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);
    setListAdapter(mediaAdapter);
}

我可以用什么代替XXX ......

private class MediaCursorAdapter extends SimpleCursorAdapter{
 @SuppressWarnings("deprecation")
public MediaCursorAdapter(Context context, int layout, Cursor c) {
    super(context, layout, c, new String[] { MediaStore.MediaColumns.DISPLAY_NAME,                    MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
    new int[] { R.id.displayname, R.id.title, R.id.duration });
    }
}

任何人都可以帮助我

提前致谢

1 个答案:

答案 0 :(得分:1)

您应该将MediaStore.Images.Media.EXTERNAL_CONTENT_URI用于XXX,

String[] projection = {    
    MediaStore.Audio.Media._ID,
    MediaStore.Audio.Media.DISPLAY_NAME,
    MediaStore.Audio.Media.DURATION
};
Cursor cursor = getActivity().getContentResolver().query(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    projection, // Which columns to return

    MediaStore.Audio.Media.DATA + " like ? ", // Return all rows
    new String[] {"%/path/to/my/files/%"},

    // or: 
    // MediaStore.Images.Media.DATA + "=? ",
    // new String[] { filePath }, 

    null);