I am facing following problems with given code:
These problems are not present for video files(else part of outer if)
Inside videoadapter:
if(videoFiles.get(position).getType().equals(MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO+"")){
if(coverpicture(videoFiles.get(position).getPath())!=null) {
Glide.with(mContext)
.load(coverpicture(videoFiles.get(position).getPath()))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(circularProgressDrawable)
.into(holder.thumbnail);
}
else {
Glide.with(mContext)
.load(new File(videoFiles.get(position).getPath()))
.placeholder(R.drawable.ic_baseline_music_note)
.into(holder.thumbnail);
}
} else {
Glide.with(mContext)
.load(new File(videoFiles.get(position).getPath()))
.placeholder(circularProgressDrawable)
.into(holder.thumbnail);
}
This is how I call videoadapter:
videoAdapter = new VideoAdapter(getActivity(),videoFiles);
The function coverpicture:
private Bitmap coverpicture(String path) {
final MediaMetadataRetriever[] mr = new MediaMetadataRetriever[1];
final byte[][] byte1 = new byte[1][1];
Thread ttt = new Thread(){
@Override
public void run() {
super.run();
mr[0] = new MediaMetadataRetriever();
mr[0].setDataSource(path);
byte1[0] = mr[0].getEmbeddedPicture();
mr[0].release();
}
};
ttt.start();
while(true){
if(!ttt.isAlive()){
if(byte1[0] != null) {
return BitmapFactory.decodeByteArray(byte1[0], 0, byte1[0].length);
}
else {
return null;
}
}
}
}
EDit: wrong thumbnail problem is solved when I replace first glide statement with
(it would be useful if someone explained why)
Glide.with(mContext)
.load(coverpicture(videoFiles.get(position).getPath()))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(circularProgressDrawable)
.into(new DrawableImageViewTarget(holder.thumbnail));
new Thread(()->{
Bitmap b =coverpicture(videoFiles.get(position).getPath());
Glide.with(mContext).downloadOnly().load(b);
holder.thumbnail.post(() -> {
Glide.with(mContext).clear(holder.thumbnail);
Glide.with(holder.thumbnail.getContext())
.load(new File(videoFiles.get(position).getPath()))
.placeholder(circularProgressDrawable)
.format(DecodeFormat.PREFER_RGB_565)
.into(new DrawableImageViewTarget(holder.thumbnail));
}
}).start();
2 . Already mentioned in the post
3 . Changed coverpicture() to
static Bitmap coverpicture(String path) {
MediaMetadataRetriever mr;
byte[] byte1 = new byte[1];
mr = new MediaMetadataRetriever();
mr.setDataSource(path);
try {
byte1 = mr.getEmbeddedPicture();
mr.release();
}catch (Exception e){
e.printStackTrace();
}
if(byte1 != null) {
return BitmapFactory.decodeByteArray(byte1, 0, byte1.length);
}
else {
return null;
}
}
No need of threading inside coverpicture() as it is called from a background thread
ALSO, the following optimistaions in recyclerview will help
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);