android自动获取SD卡中音乐列表

  首先大家要了解MediaStore这个类,MediaStore是android系统提供的一个多媒体数据库,android中多媒体信息都可以从这里提取。这个MediaStore包括了多媒体数据库的所有信息,包括音频,视频和图像,android把所有的多媒体数据库接口进行了封装,所有的数据库不用自己进行创建,直接调用利用ContentResolver去掉用那些封装好的接口就可以进行数据库的操作了。

以下是我测试的代码:

main.xml  这个没什么意思

ContractedBlock.gif ExpandedBlockStart.gif View Code
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:text="Button1"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="Button2"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>



MediaContentproviderActivity.java 类

ContractedBlock.gif ExpandedBlockStart.gif View Code
 1 package hzx.one;
2
3 import hzx.three.Song;
4 import java.util.ArrayList;
5
6 import android.app.Activity;
7 import android.content.BroadcastReceiver;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.IntentFilter;
11 import android.database.Cursor;
12 import android.net.Uri;
13 import android.os.Bundle;
14 import android.os.Environment;
15 import android.provider.MediaStore;
16 import android.util.Log;
17 import android.view.View;
18 import android.widget.Button;
19
20 public class MediaContentproviderActivity extends Activity {
21 private Button button1;
22 private Button button2;
23 private static final String TAG="showsong";
24 @Override
25 public void onCreate(Bundle savedInstanceState) {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.main);
28 button1=(Button) findViewById(R.id.button1);
29 button1.setOnClickListener(new View.OnClickListener() {
30 @Override
31 public void onClick(View v) {
32 // TODO Auto-generated method stub
33 //Log.d(TAG, readDataFromSD(MediaContentproviderActivity.this).toString());
34 readDataFromSD(MediaContentproviderActivity.this);
35 }
36 });
37 button2=(Button) findViewById(R.id.button2);
38 button2.setOnClickListener(new View.OnClickListener() {
39 @Override
40 public void onClick(View v) {
41 // TODO Auto-generated method stub
42 //Log.d(TAG, readDataFromSD(MediaContentproviderActivity.this).toString());
43 Log.d(TAG,"update list");
44 scanSdCard();
45 }
46 });
47 }
48 public static ArrayList<Song> readDataFromSD(Context context){
49 Log.d(TAG, "scanFile");
50 Cursor cursor = context.getContentResolver().query(
51 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
52 new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA}
53 , MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=?", new String[]{"audio/mpeg","audio/x-ms-wma"},null);
54
55 while (cursor.moveToNext()) {
56 Log.d(TAG, "show--->"+cursor.getString(1));
57 }
58 return null;
59 }
60 private class ScanSdFilesReceiver extends BroadcastReceiver {
61 @Override
62 public void onReceive(Context arg0, Intent arg1) {
63 // TODO Auto-generated method stub
64 String action = arg1.getAction();
65 if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action)) {
66 //当系统开始扫描sd卡时,为了用户体验,可以加上一个等待框
67 //scanHandler.sendEmptyMessage(STARTED);
68 Log.d(TAG, "update--->started");
69 }
70 if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)) {
71 //当系统扫描完毕时,停止显示等待框,并重新查询ContentProvider
72 //scanHandler.sendEmptyMessage(FINISHED);
73 Log.d(TAG, "update--->finished");
74 }
75 }
76 }
77 private void scanSdCard() {
78 Log.d(TAG, "intent---> start");
79 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
80 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
81 intentFilter.addDataScheme("file");
82 ScanSdFilesReceiver scanReceiver = new ScanSdFilesReceiver();
83 registerReceiver(scanReceiver, intentFilter);
84 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
85 Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));
86 Log.d(TAG, "intent---> finish");
87 }
88 }

运行效果:

初始

当我们按下button1时:

而后我们加入一首歌到cd卡中,然后按button2刷新列表,再按button1:

转载于:https://www.cnblogs.com/zhukoo/archive/2011/09/25/2190432.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值