gpt4 book ai didi

android - Recyclerview 图像在向上/向下滚动时错位并消失

转载 作者:行者123 更新时间:2023-11-29 17:01:10 26 4
gpt4 key购买 nike

我正在检索视频缩略图并将其显示在回收 View 的 ImageView 中,我面临的问题是图像在向上/向下滚动时错位并消失。当我使用可运行类时,Recyclarview 会卡住。

private static Bitmap retrieveVideoFrameFromVideo(String videoPath) {
Bitmap bitmap = null;
MediaMetadataRetriever mediaMetadataRetriever = null;
try {
mediaMetadataRetriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14) {
mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
} else {
mediaMetadataRetriever.setDataSource(videoPath);
}
// mediaMetadataRetriever.setDataSource(videoPath);
bitmap = mediaMetadataRetriever.getFrameAtTime();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mediaMetadataRetriever != null) {
mediaMetadataRetriever.release();
}
}
return bitmap;
}

@Override void bindData(RecyclerView.ViewHolder holder, final MediaResponse data) {
final PhotoHolder photoHolder = (PhotoHolder) holder;
photoHolder.mTitle.setText(BasicUtils.removeDoubleQuotes(data.getTitle()));
photoHolder.mPostedBy.setText(data.getPostedby());
photoHolder.upvote.setText(String.valueOf(data.getLikes()));
photoHolder.date.setText(
BasicUtils.parseDate(data.getDatetime(), AppConstantsUtils.FROM_DATE_FORMAT,
AppConstantsUtils.TO_DATE_FORMAT));


new BackTask(photoHolder.mMediaPic).execute(
AppConstantsUtils.BASE_URL + data.getMedia());




}

private class BackTask extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
BackTask(ImageView imageView) {
this.imageView = imageView;
}

@Override protected Bitmap doInBackground(String... params) {
// return retrieveVideoFrameFromVideo(params[0]);
Log.d("URLHARRY",params[0]);
// return ThumbnailUtils.createVideoThumbnail(params[0], MediaStore.Video.Thumbnails.MINI_KIND );
return retrieveVideoFrameFromVideo(params[0]);
}

@Override protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);

if(bitmap!=null) {
imageView.setImageBitmap(bitmap);
}
else{
imageView.setImageBitmap(null);
ToastUtils.showToast(context,"Hai");
}

}

最佳答案

在 Viewholder 中使用 AsynTask

 new AsyncTask<String, String, String>() {
Bitmap bitmapVideo;
String location;


@Override
protected String doInBackground(String... strings) {
try {
//Your method call here
bitmapVideo = retrieveVideoFrameFromVideo(strings[0]);
location=strings[0];
// addBitmapToMemoryCache(location, bitmapVideo);

} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String id) {
super.onPostExecute(id);

if (bitmapVideo != null) {
//Load your bitmap here
photoHolder.mMediaPic.setImageBitmap(bitmapVideo);
addBitmapToMemoryCache(location, bitmapVideo);


} else {
Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ananda);
photoHolder.mMediaPic.setImageBitmap(largeIcon);
addBitmapToMemoryCache(location, largeIcon);

}
}
}.execute(AppConstantsUtils.BASE_URL + data.getMedia());

关于android - Recyclerview 图像在向上/向下滚动时错位并消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42943593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com