缩略图查询获取图片存在延迟大的问题,通过自定义anysctask实现其doInBackground并在其onPostExecute方法中回调设置缩略图实现异步更新缩略图功能。
private class MyTask extends AsyncTask<Object, Integer, Bitmap> {
public final Uri uriImage = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
public final Uri uriVideo = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
public String[] projectionImage = {MediaStore.Images.Media.DATA};
public String[] projectionVideo = {MediaStore.Video.Media.DATA};
private Context mContext;
public MyTask(Context context) {
mContext = context;
}
@Override
protected Bitmap doInBackground(Object... objects) {
@SuppressLint("Recycle")
Cursor cursorImage = mContext.getContentResolver().query(uriImage, projectionImage,
null, null, MediaStore.Images.Media.DATE_ADDED + " DESC");
Cursor cursorVideo = mContext.getContentResolver().query(uriVideo, projectionVideo,
null, null, MediaStore.Video.Media.DATE_ADDED + " DESC");
Bitmap resultBitmap = FileUtil.compareImageAndVideoPath(cursorImage, cursorVideo);
return resultBitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
Log.d(TAG, "onPostExecute: video中获取缩略图成功");
btnImagePreview.setImageBitmap(bitmap);
}
}
}