![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqYhQUtPyn7wowH9USRQfmPgsobbCM1gmhH4hki6gmnD8r6k_htvc5SWqqkjB2vN6t0vb490f1bbmCN3UeW92d5WaabCia5fkdoABUpxHqUUvNlGeS7jCb0jHj1BnmIGyECNS7qnJHmZq_/s400/ListMedia_04.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBUExlYZ-KvBgRxR30APVR5T17KZfxwuC36K4wHwAHpRl2wmEOPf0kJGVtXIsLvSqfYj1RNbJHpIFSn9GiOJ3Dgm2TSnbK9Mvnt17xIMSC-TWgnqyrKYQjVhh3mmhrFinuezj8iq_ZufF4/s400/ListMedia_05.png)
package com.exercise.AndroidListMedia;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class AndroidListMediaActivity extends ListActivity {
SimpleCursorAdapter adapter;
final Uri mediaSrc = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = {
MediaStore.MediaColumns.TITLE};
int[] to = {
android.R.id.text1};
Cursor cursor = managedQuery(
mediaSrc,
null,
null,
null,
MediaStore.Audio.Media.TITLE);
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, from, to);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = adapter.getCursor();
cursor.moveToPosition(position);
String _id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
Uri playableUri
= Uri.withAppendedPath(mediaSrc, _id);
Toast.makeText(this, "Play: " + playableUri.toString(), Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(playableUri);
startActivity(intent);
}
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhp-WVdVRNoSSItrVOTvl2X9CE0zg_poZkG2QR80L1lOV_t9fi4v9to6WR4WzEi5c3VfhqlH-gQHma-08CHP-Fl3WF3LkFYYfR_d_bIyKuI_wKRIX6jR6qiRpd986VnCkTyUwPToGeHyad9/s200/icons_download.gif)
Next:
- Play media on surfaceview using android.media.MediaPlayer
Remark:
Please notice that both managedQuery(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) and SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) are deprecated. See how to use CursorLoader here.
0 تعليقات