【发布时间】:2018-04-13 17:52:49
【问题描述】:
我正在使用以下代码在 android 中获取设备的音频数据
public void getSongList()
{
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
ContentResolver contentResolver = getActivity().getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri,null,null,null,null);
if(cursor!=null && cursor.moveToFirst())
{
do {
mediaMetadataRetriever.setDataSource(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
Bitmap image = null;
try
{
byte[] art = mediaMetadataRetriever.getEmbeddedPicture();
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 2;
image = BitmapFactory.decodeByteArray(art, 0, art.length, opt);
}catch(Exception e){}
mSongModel.add(new SongModel(id,title,artist,image));
}while(cursor.moveToNext());
}
}
问题是,它显着影响了启动时间。
因此,我们将不胜感激任何有关性能改进的建议。 或者,我应该为此使用异步任务吗?
谢谢。
【问题讨论】:
标签: android performance audio android-contentresolver mediastore