先来了解一下ContentResolver的查询接口,和sqlite数据库查询的方法是一样的
通过 query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder); 得到一个Cursor对象,这个Cursor对象中有数据库字段对应相应的音乐的信息:
Uri:指明要查询的数据库名称加上表的名称,从MediaStore中我们可以找到相应信息的参数,具体请参考开发文档。
Projection: 指定查询数据库表中的哪几列,返回的游标中将包括相应的信息。Null则返回所有信息。
selection: 指定查询条件
selectionArgs:参数selection里有 ?这个符号是,这里可以以实际值代替这个问号。如果selection这个没有?的话,那么这个String数组可以为null。
SortOrder:指定查询结果的排列顺序
下面的命令将返回所有在外部存储卡上的音乐文件的信息:
先得到一个ContentResolver对象:ContentResolver cr = this.getContentResolver();
Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);得到cursor后,我们可以调用Cursor的相关方法具体的音乐信息:
歌曲ID:MediaStore.Audio.Media._ID
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
歌曲的名称 :MediaStore.Audio.Media.TITLE
String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
歌曲的歌手名: MediaStore.Audio.Media.ARTIST
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
歌曲文件的路径 :MediaStore.Audio.Media.DATA
String dataurl = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
歌曲的总播放时长 :MediaStore.Audio.Media.DURATION
int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
歌曲文件的大小 :MediaStore.Audio.Media.SIZE
long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
下面看我写的小程序的截图:
程序的开始界面: 点击button按钮后的界面:
点击选择曲目后的界面: 点击情歌后的界面:
下面看代码:在NotificationActivity工程下面
在com.cn.daming包下的NotificationActivity.java的代码:
package com.cn.daming; import android.app.Activity; import android.content.ContentResolver; 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.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class NotificationActivity extends Activity { private Button mButton2; private TextView textview3; private static final int MUSIC_PICKED = 3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textview3 = (TextView)findViewById(R.id.textview3); mButton2 = (Button)findViewById(R.id.button2); mButton2.setOnClickListener(new OnClickListener(){ public void onClick(View v) { Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); innerIntent.setType("audio/*"); // innerIntent.setType("audio/mp3"); // innerIntent.setType("audio/midi"); Intent wrapperIntent = Intent.createChooser(innerIntent, null); startActivityForResult(wrapperIntent, MUSIC_PICKED); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode != RESULT_OK) { return; } else { String mCustomRingtone = null; if(requestCode == MUSIC_PICKED){ Uri pickedUri = data.getData(); if (pickedUri != null) { mCustomRingtone = pickedUri.toString(); ContentResolver cr = this.getContentResolver(); Cursor cursor = cr.query(pickedUri, null, null, null, null); cursor.moveToFirst(); String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)); textview3.setText("mCustomRingtone:"+mCustomRingtone+ "\n\n pickedUri.getPath()=" +pickedUri.getPath() + "\n\n file url ="+url+ "\n\n file title="+title+ "\n\n file singer = "+artist+ "\n\n music duration="+duration+ "\n\n file size="+size); }else{ textview3.setText("null:"); } } } } }
在layout包下的main.xml中的代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginTop="20dip" android:text="@string/hello" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:text="调用内存卡铃声" /> <TextView android:id="@+id/textview3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dip" /> </LinearLayout>
在res下的string.xml中的代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">大明原创,音乐信息</string> <string name="app_name">NotificationApp</string> </resources>有问题的可以留言,欢迎大家点评,以纠正错误!