android 把SD卡上的音乐文件用ListActivity显示出来 (eclipse)

时间:2022-11-03 15:23:09

啥也不说了 先来个效果图

发这个博的原因是我在网上找了很久才找到类似的,一直搞不明白,走了不少弯路

希望能帮到有需要的人

android  把SD卡上的音乐文件用ListActivity显示出来 (eclipse)


xml文件内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="1" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:textColor="#000000"
android:textSize="20sp"
android:text="1" />

<TextView
android:id="@+id/tv_singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:text="1" />

<TextView
android:id="@+id/tv_album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="1" />

<TextView
android:id="@+id/tv_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="1" />

<TextView
android:id="@+id/tv_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="1" />

<TextView
android:id="@+id/tv_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="1" />

</LinearLayout>
</LinearLayout>



Java文件内容

package com.example.mymusicvideo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

mysongshow();


}

public void mysongshow(){
ContentResolver cr = getApplication().getContentResolver();
if (cr == null) {
return;
}
// 获取所有歌曲
Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
if (null == cursor) {
return;
}

//获取到集合数据

List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();


if (cursor.moveToFirst()) {
do{
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String singer = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
int duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

HashMap<String, Object> item = new HashMap<String, Object>();
item.put("music_id", id);
item.put("music_title", title);
item.put("music_singer", singer);
item.put("music_album",album);
item.put("music_size", size);
item.put("music_duration", duration);
item.put("music_path", url);
data.add(item);
}while(cursor.moveToNext());
}

String[] from = {"music_id","music_title","music_singer","music_album","music_size","music_duration","music_path"};
int[] to= {R.id.tv_id,R.id.tv_title,R.id.tv_singer,R.id.tv_album,R.id.tv_size,R.id.tv_duration,R.id.tv_url};
@SuppressWarnings("deprecation")
SimpleAdapter adapter = new SimpleAdapter(this,data
,R.layout.activity_main,from,to);
ListView listView = getListView();
listView.setAdapter(adapter);
// listView.setBackgroundColor(Color.parseColor("#000000"));//设置界面即listView的背景颜色为黑色
listView.setBackgroundResource(R.drawable.listview_background);//为界面添加背景图片
listView.setDividerHeight(50);//分割线的高度
//处理图片大小
Drawable drawable1 = getResources().getDrawable(R.drawable.a007);
drawable1.setBounds(2, 2, 2, 2);//第一0是距左边距离,第二0是距上边距离,40分别是长宽
//设置分割线的背景图片
listView.setDivider(drawable1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


获取SD卡权限(十分重要,这是获取音乐文件的钥匙)添加红色代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mymusicvideo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>