Rektirino
Rektirino

Reputation: 582

Multiple selectionArgs - query to mediaStore

I'm trying to show audio files from specific folders. The folders are selected by user and added to arraylist, hence arraylist is dynamic. Now, when I query for multiple folders the mediaStore does not return any results. I think the problem is with my selection. I know the number of items in selectionArgs should match the ? in selection, so I created a function that does that, but no luck. Hope you guys can help me. Thank you !

Below is the code:

    String[] whereVal;
    String selection;

 Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;



        String[] folderPaths = storage.loadFolderPaths().toArray(new String[storage.loadFolderPaths().size()]);
        selection = MediaStore.Audio.Media.DATA +" IN (" + makePlaceholders(folderPaths.length) + ")" ;
        whereVal = folderPaths;


   String[] projection = {MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.ALBUM_ID};
    Cursor cursor = getActivity().getContentResolver().query(uri, projection, selection, whereVal, null);



private String makePlaceholders(int len) {

    StringBuilder sb = new StringBuilder(len * 2 - 1);
    sb.append("?");
    for (int i = 1; i < len; i++)
        sb.append(",?");
    Log.e("len: ", String.valueOf(len));
    Log.e("Selection: ",sb.toString());
    return sb.toString();

}

Upvotes: 0

Views: 890

Answers (1)

Theo
Theo

Reputation: 2042

In addition to my comment:

            // strip trackname
    path = path.substring(path.lastIndexOf("/")+1,path.length());
    String where = MediaStore.Audio.Media.DATA + " LIKE " + "\'%" +path+"%\'";

Upvotes: 1

Related Questions