Android 媒体扫描时间过长
Android Media Scan takes too long
我正在使用下面的代码来 scan all media/mp3
设备上的文件,但是当设备上有很多音乐时需要很长时间。
我想知道是否有更快的方法来获取和显示所有音乐?
注意:该代码不会导致崩溃或挂断或任何其他问题...
class GetLocals extends AsyncTask<Void,Void,Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
SongCover_lc.clear();
SongID_lc.clear();
Singer_lc.clear();
Path_lc.clear();
AlbumName_lc.clear();
SongName_lc.clear();
}
@Override
protected Void doInBackground(Void... voids) {
try {
String[] STAR = {"*"};
Cursor cursor;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = context.getContentResolver().query(uri, STAR, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String albumName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String singer = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String songid = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(path);
byte[] artBytes = mmr.getEmbeddedPicture();
if (artBytes != null) {
InputStream is = new ByteArrayInputStream(mmr.getEmbeddedPicture());
Bitmap bm = BitmapFactory.decodeStream(is);
SongCover_lc.add(bm);
} else {
SongCover_lc.add(BitmapFactory.decodeResource(getResources(), R.color.white));
}
SongName_lc.add(songName);
AlbumName_lc.add(albumName);
Path_lc.add(path);
Singer_lc.add(singer);
SongID_lc.add(songid);
} while (cursor.moveToNext());
}
}
} catch (Exception e){
System.out.println("CAAAAAAAAAAATCH");
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
lazyAdapter_localSongs = new LazyAdapter_LocalSongs(activity,SongName_lc,Singer_lc,SongCover_lc,AlbumName_lc,Path_lc);
listviewloclas.setAdapter(lazyAdapter_localSongs);
}
}
> I was wondering if there is a faster way to get
> and show all musics ...
将基于阵列的适配器替换为 CursorAdapter 以按需加载歌曲。
因此,与其在 listview/grid 可用之前将 1000 首歌曲预加载到数组中,不如一次加载 10 首可见的歌曲。
我正在使用下面的代码来 scan all media/mp3
设备上的文件,但是当设备上有很多音乐时需要很长时间。
我想知道是否有更快的方法来获取和显示所有音乐?
注意:该代码不会导致崩溃或挂断或任何其他问题...
class GetLocals extends AsyncTask<Void,Void,Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
SongCover_lc.clear();
SongID_lc.clear();
Singer_lc.clear();
Path_lc.clear();
AlbumName_lc.clear();
SongName_lc.clear();
}
@Override
protected Void doInBackground(Void... voids) {
try {
String[] STAR = {"*"};
Cursor cursor;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = context.getContentResolver().query(uri, STAR, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String albumName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String singer = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String songid = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(path);
byte[] artBytes = mmr.getEmbeddedPicture();
if (artBytes != null) {
InputStream is = new ByteArrayInputStream(mmr.getEmbeddedPicture());
Bitmap bm = BitmapFactory.decodeStream(is);
SongCover_lc.add(bm);
} else {
SongCover_lc.add(BitmapFactory.decodeResource(getResources(), R.color.white));
}
SongName_lc.add(songName);
AlbumName_lc.add(albumName);
Path_lc.add(path);
Singer_lc.add(singer);
SongID_lc.add(songid);
} while (cursor.moveToNext());
}
}
} catch (Exception e){
System.out.println("CAAAAAAAAAAATCH");
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
lazyAdapter_localSongs = new LazyAdapter_LocalSongs(activity,SongName_lc,Singer_lc,SongCover_lc,AlbumName_lc,Path_lc);
listviewloclas.setAdapter(lazyAdapter_localSongs);
}
}
> I was wondering if there is a faster way to get
> and show all musics ...
将基于阵列的适配器替换为 CursorAdapter 以按需加载歌曲。
因此,与其在 listview/grid 可用之前将 1000 首歌曲预加载到数组中,不如一次加载 10 首可见的歌曲。