前段时间处理android音乐文件信息,上网查资料发现Android系统自己提供了MediaScanner,MediaProvider,MediaStore等接口并且提供了一套数据库表格,通过 Content Provider的方式把内容共享给用户。说明一下【Android数据是私有的】可以通过Content Provider的方式共享数据,前面我大致介绍了这个Content Provider,Android中ContentProvider简介【安卓进化二十七】 。当手机开机或者有SD卡插拔等事件发生时,系统将会自动扫描SD卡和手机内存上的媒体文件,如 audio,video,图片等,将相应的信息放到定义好的数据库表格中。如果不插拔手机内存卡,如果把相应的音乐文件删除或移到别的文件夹中,系统就不会自动扫描手机内存卡,查询的Cursor对象存在,但是cursor.getCount()的值为0。在这个程序中,我们不需要关心如何去扫描手机中的文件,只要了解如何查询和使用这些信息就可以了。MediaStore中定义了一系列的数据表格,(这个数据表格是android系统自己建立的,我们不用关心如何建立,我们关心如何使用就可以了!)通过ContentResolver提供的查询接口,我们可以得到各种需要的信息。

先来了解一下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);}        });    }@Overrideprotected 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>
有问题的可以留言,欢迎大家点评,以纠正错误!

   

更多相关文章

  1. Android(安卓)Dialog的7种形式
  2. Android(安卓)Push Notification实现信息推送使用
  3. [转]Android媒体的一些使用总结
  4. 使用java获取未来7天天气信息,可用于android
  5. Tabhost中Activity绑定Service
  6. Android根据包名取得指定程序包的信息(名称、图标……)
  7. Android(安卓)- 手机开发调试无法输出logcat信息 - 未解决
  8. android的PreferenceActivity
  9. android的PreferenceActivity

随机推荐

  1. android本地化和国际化
  2. Android(安卓)studio异常记录
  3. Android: 获取当前线程状态
  4. Mac 进行 android 真机调试
  5. android给图片添加边框
  6. Android(安卓)DownloadManager的用法
  7. android UI 设计之 Tabs
  8. Android聊天背景图片变形解决方案
  9. Android(安卓)VSYNC详解
  10. Android常用库