16 读取音乐文件
所需权限
<!--Sd读写权限 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
java代码:
package com.qf.day16_contentresolver_music_demo3; import java.io.File; import android.annotation.SuppressLint; 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.provider.MediaStore.Audio.Media; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; /** * * 获取音乐 * */ public class MainActivity extends Activity { private ListView lv; private TextView tv; private ContentResolver contentResolver; //获取音频的Uri对象 private Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; SimpleCursorAdapter adapter; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); tv = (TextView) findViewById(R.id.tv); contentResolver = getContentResolver(); //获取Sd卡里音乐的数据 //Media._ID 如果要用SimpleCursorAdaptet必须要查找此字段 //Media.DISPLAY_NAME 音乐名 XXX.MP4 //Media.DATA 绝对路径 //Media.TITLE 音乐名 XXX Cursor cursor = contentResolver.query(uri, new String[]{Media._ID, Media.DISPLAY_NAME, Media.DATA, Media.TITLE}, null, null, null); adapter = new SimpleCursorAdapter (MainActivity.this, R.layout.item, cursor, new String[]{Media.DISPLAY_NAME, Media.DATA, Media.TITLE}, new int[]{R.id.tv_name,R.id.tv_data,R.id.tv_title}, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); lv.setAdapter(adapter); lv.setEmptyView(tv); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub //获取当前位置的数据 Cursor c = (Cursor) adapter.getItem(position); //获取音乐的 路径 String path = c.getString(c.getColumnIndex(Media.DATA)); //播放音乐 Intent intent= new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(path)), "audio/*"); startActivity(intent); } }); } }
layout 布局代码:
activity_main2.xml:<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:textColor="#f00" android:text="暂无数据" /> </RelativeLayout>
item.xml:
填充list布局<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv" android:singleLine="true" android:text="aaa" /> <TextView android:id="@+id/tv_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_name" android:layout_toRightOf="@id/iv" android:singleLine="true" android:text="aaa" /> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_data" android:layout_toRightOf="@id/iv" android:singleLine="true" android:text="aaa" /> </RelativeLayout>