package com.annimon.simplypaint; import android.app.ListActivity; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.widget.ListAdapter; import android.widget.SimpleCursorAdapter; import android.widget.Toast; import java.util.ArrayList; import java.util.Random; /** * * @author aNNiMON */ public class GetPlaylist extends ListActivity { /** * Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); String[] projection = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE }; String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0 "; ArrayList paths = new ArrayList(); Cursor musicListInternalMemoryCursor = query(this, MediaStore.Audio.Media.INTERNAL_CONTENT_URI, projection, selection, null, null, 0); if (musicListInternalMemoryCursor != null) { for (int i = 0; i < musicListInternalMemoryCursor.getCount(); i++) { musicListInternalMemoryCursor.moveToPosition(i); String p = musicListInternalMemoryCursor.getString(1); if (p.endsWith("mp3")) paths.add(p); } musicListInternalMemoryCursor.close(); } Cursor musicListSDCardCursor = query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null, 0); if (musicListSDCardCursor != null) { for (int i = 0; i < musicListSDCardCursor.getCount(); i++) { musicListSDCardCursor.moveToPosition(i); String p = musicListSDCardCursor.getString(1); if (p.endsWith("mp3")) paths.add(p); } //musicListSDCardCursor.close(); } ListAdapter adapter = new SimpleCursorAdapter(this, // Context. android.R.layout.two_line_list_item, musicListSDCardCursor, // Pass in the cursor to bind to. // Array of cursor columns to bind to. new String[] { MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA }, // Parallel array of which template objects to bind to those // columns. new int[] { android.R.id.text1, android.R.id.text2 }, 0); // Bind to our new adapter. setListAdapter(adapter); if (paths.isEmpty()) { Toast.makeText(this, "no media found", Toast.LENGTH_LONG).show(); return; } Random r = new Random(); int pos = r.nextInt(paths.size() - 1); if (paths.size() == 1) pos = 0; String url = paths.get(pos); if (url.length() == 0) return; Toast.makeText(this, url, Toast.LENGTH_LONG).show(); try { Intent it = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("file://" + url); it.setDataAndType(uri, "audio/mpeg"); startActivity(it); } catch (Exception e) { Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); } } private static Cursor query(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, int limit) { try { ContentResolver resolver = context.getContentResolver(); if (resolver == null) { return null; } if (limit > 0) { uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build(); } return resolver.query(uri, projection, selection, selectionArgs, sortOrder); } catch (UnsupportedOperationException ex) { return null; } } }