ContentProviderMedia获取 外部存储的 音频文件

时间:2022-02-11 16:27:52

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.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import java.io.File;

/**
* 获取 外部存储的 音频文件
* @author alice
*
*/
public class MainActivity extends AppCompatActivity {
private ListView listView;

// 获取外部存储的 音频文件 也可以 修改为 视频 Vedio 或内部存储
private Uri mediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

private SimpleCursorAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);

ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(mediaUri,
new String[] { MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE }, null, null, null);

adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.item_layout, cursor,
new String[] { MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE },
new int[] { R.id.diaplayname_tv, R.id.data_tv, R.id.title_tv },
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.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(MediaStore.Video.Media.DATA));

// 系统播放音频文件
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "audio/*");
startActivity(intent);
}
});

}

}

MainActivity的布局文件

<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"
tools:context="alice.bw.com.day16contentprovidermedia.MainActivity">



<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</ListView>
</RelativeLayout>

item_layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/diaplayname_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

<TextView
android:id="@+id/data_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

<TextView
android:id="@+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

</LinearLayout>

权限

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="alice.bw.com.day16contentprovidermedia">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

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

</manifest>