2014-11-1
音频作业:要求 查询媒体的表 异步查询
①Sdcard目录Mnt=========>sdcard放入音频文件
②在data/data/...下找到com.android.provider.media
把导出外部存储数据库(externalXXX.db),找到相应的音频数据库表,来进行查询,并且显示到ListView当中
③查看是否需要加入权限
④查看API文档
使用的类在android.provider包下找
============》MediaStore.Audio
============》MediaStore.Audio.Media
拿到CONTENT_URI(外部)
⑤注意事项
One。当你导入视频或者音频到sdcard下后,发现数据库中不存在数据
则,你必须重启模拟器。之后,再导出进行看看
Two。如果在Android中找不到相对应的常量的数据库列的名字
就直接写数据库的列名就行了
Three。音频文件名不要用中文
===============================================================
接下来是我写的作业咯~
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/myListView"></ListView>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/videoName_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="@+id/atistName_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.aa.audio;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio.Artists;
import android.provider.MediaStore.Audio.Media;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
ListView myListView;
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
MySyncQueryHandler queryHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
queryHandler = new MySyncQueryHandler(getContentResolver());
queryHandler.startQuery(0,
null,
Media.EXTERNAL_CONTENT_URI,
new String[]{Media.ARTIST_ID,Media.DISPLAY_NAME,Media._ID},
null,
null,
null);
initView();
}
private void initView() {
myListView=(ListView)findViewById(R.id.myListView);
SimpleAdapter simpleadapter=new SimpleAdapter(
MainActivity.this,
list,
R.layout.list_item,
new String[]{"videoName","artistName"},
new int[]{R.id.videoName_tv,R.id.atistName_tv});
myListView.setAdapter(simpleadapter);
}
class MySyncQueryHandler extends AsyncQueryHandler{
public MySyncQueryHandler(ContentResolver cr) {
super(cr);
// TODO Auto-generated constructor stub
}
@Override
protected void onDeleteComplete(int token, Object cookie, int result) {
// TODO Auto-generated method stub
super.onDeleteComplete(token, cookie, result);
}
@Override
protected void onInsertComplete(int token, Object cookie, Uri uri) {
// TODO Auto-generated method stub
super.onInsertComplete(token, cookie, uri);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
cursor.moveToFirst();
while(cursor.moveToNext()){
Uri uri1=Uri.parse(Artists.EXTERNAL_CONTENT_URI.toString());
String id=cursor.getString(cursor.getColumnIndex(Media.ARTIST_ID));
String name=cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME));
Cursor c1=getContentResolver().query(uri1,
new String[]{Artists._ID,Artists.ARTIST},
Artists._ID+"=?",
new String[]{id},
null);
Map<String,Object> map=new HashMap<String,Object>();
map.put("_id", Integer.parseInt(id));
map.put("videoName", name);
while(c1.moveToNext()){
map.put("artistName", c1.getString(c1.getColumnIndex(Artists.ARTIST)));
list.add(map);
}
c1.close();
}
cursor.close();
super.onQueryComplete(token, cookie, cursor);
}
@Override
protected void onUpdateComplete(int token, Object cookie, int result) {
// TODO Auto-generated method stub
super.onUpdateComplete(token, cookie, result);
}
}
}
效果图