國慶七天沒有更新博客,今天第一天上班就來了。android
想了好久不知道寫什麼,忽然想了最近在作的項目要用的一個功能(利用contentProvider來掃描內存卡上的音頻文件),速度至關給力,300首歌一秒鐘都不到。廢話很少說,上代碼吧!ide
主界面:工具
public class MainActivity extends Activity {佈局
private ListView listview;
private List<MyAudio> data;
private BaseAdapter adapter ;
private RelativeLayout root ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = (RelativeLayout) findViewById(R.id.root) ;
data = ReadDataFromContentProvider.readAudio(this) ;
if(null == data || data.size() == 0) {
Toast.makeText(this, "沒有掃描到音頻文件!", Toast.LENGTH_LONG).show() ;
return ;
}
listview = (ListView) findViewById(R.id.listview) ;
adapter = new AudioAdapter(this, data) ;
listview.setAdapter(adapter) ;
listview.setOnItemClickListener(new OnItemClickListener() {this
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// 獲取專輯圖片
Bitmap bmp = data.get(arg2).getAlbumCover() ;
if(null != bmp) {
BitmapDrawable drawable = new BitmapDrawable(getResources(), bmp) ;
root.setBackgroundDrawable(drawable);
}
else {
root.setBackgroundColor(Color.GRAY) ;
}
}
}) ;
ReadDataFromContentProvider.readImage(this) ;
}
}code
class AudioAdapter extends BaseAdapter {
private Context context ;
private List<MyAudio> data ;
圖片
public AudioAdapter(Context context, List<MyAudio> data) {
super();
this.context = context;
this.data = data;
}內存
@Override
public int getCount() {
return data.size() ;
}get
@Override
public Object getItem(int position) {
return data.get(position) ;
}博客
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view ;
if(null != convertView) {
view = convertView ;
}
else {
view = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_2, null) ;
}
TextView tv1 = (TextView) view.findViewById(android.R.id.text1) ;
tv1.setText(data.get(position).getTitle()) ;
TextView tv2 = (TextView) view.findViewById(android.R.id.text2) ;
tv2.setText(data.get(position).getArtist() + "\t" + data.get(position).getAlbum()) ;
return view;
}
}
實體類:
public class MyAudio {
private String title , path , artist , album ;
private long duration ;
private Bitmap albumCover ;
public MyAudio(String title, String path, String artist, String album , long duration) {
super();
this.title = title;
this.path = path;
this.artist = artist;
this.album = album;
this.duration = duration ;
}
public MyAudio(String title, String path, String artist, String album , long duration , Bitmap albumCover) {
super();
this.title = title;
this.path = path;
this.artist = artist;
this.album = album;
this.duration = duration ;
this.albumCover = albumCover ;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public Bitmap getAlbumCover() {
return albumCover;
}
@Override
public String toString() {
return "MyAudio [title=" + title + ", path=" + path + ", artist="
+ artist + ", album=" + album + ", duration=" + duration
+ ", albumCover=" + albumCover + "]";
}
}
工具類:
public class ReadDataFromContentProvider {
public static void readImage(Context context) {
ContentResolver resolver = context.getContentResolver() ;
Uri imgUri = Images.Media.EXTERNAL_CONTENT_URI ;
String[] columns = new String[]{
Images.Media.TITLE ,
Images.Media.DATA ,
Images.Media.SIZE ,
Images.Media.MIME_TYPE // 類型
};
Cursor cursor = resolver.query(imgUri, columns, null, null, null) ;
if(null != cursor && cursor.getCount() > 0) {
while(cursor.moveToNext()) {
Log.e("imgs" , cursor.getString(0) + "\t" + cursor.getString(3)) ;
}
}
if(null != cursor) {
cursor.close() ;
}
}
public static List<MyAudio> readAudio(Context context) {
ContentResolver resolver = context.getContentResolver() ;
Uri audioUri = Audio.Media.EXTERNAL_CONTENT_URI ;
String[] columns = {
Audio.Media.TITLE , // 歌名
Audio.Media.DATA , // 路徑
Audio.Media.ARTIST , // 演唱者
Audio.Media.ALBUM , // 專輯名
Audio.Media.DURATION , // 音頻文件長度,毫秒
Audio.Media.ALBUM_KEY // 用來讀取專輯圖片時使用
};
List<MyAudio> data = null ;
Cursor cursor = resolver.query(
audioUri,
columns,
//null, null,
Audio.Media.DATA + " like ? or " + Audio.Media.DATA + " like ?" , new String[]{"%.mp3" , "%.wma"} ,
null) ;
if(null != cursor && cursor.getCount() > 0) {
data = new ArrayList<MyAudio>() ;
while(cursor.moveToNext()) {
// 讀到一個音頻
/////////////////// 讀取該音頻的專輯圖片信息
Bitmap bmp = null ;
String albumKey = cursor.getString(5) ;
Uri albumUri = Audio.Albums.EXTERNAL_CONTENT_URI ;
Cursor albumCursor = resolver.query(albumUri, new String[]{Audio.Albums.ALBUM_ART}, Audio.Albums.ALBUM_KEY +"=?", new String[]{albumKey}, null) ;
if(albumCursor != null && albumCursor.getCount() > 0) {
albumCursor.moveToNext() ;
String albumPath = albumCursor.getString(0);
bmp = BitmapFactory.decodeFile(albumPath) ;
albumCursor.close() ;
}
//////////////////
data.add(new MyAudio(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3) , cursor.getLong(4) , bmp)) ;
}
}
if(null != cursor) {
cursor.close() ;
}
return data ;
}
}
佈局文件很簡單,就是一個liseview,代碼可能有些地方寫的不是很好,歡迎你們批評指正!