I'm currently loading all videos on the device into a custom ListAdapter that shows the thumbnail for the Video + more. I've noticed as I add more and more videos it gets slower to initiate. This is how my List adapter looks like:
public class VideoListAdapter extends ArrayAdapter<Video> {
private ArrayList<Video> allVideos;
private HashMap<String, Bitmap> bitmapCache;
public VideoListAdapter(Context context, ArrayList<Video> videos) {
super(context, R.layout.video_list_item, videos);
this.allVideos = videos;
/* Cache the thumbnails */
setUpBitmaps();
}
private void setUpBitmaps() {
bitmapCache = new HashMap<String, Bitmap>(allVideos.size());
for(Video video : allVideos){
bitmapCache.put(video.getDATA(), ThumbnailUtils.createVideoThumbnail(video.getDATA(), Thumbnails.MICRO_KIND));
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(R.layout.video_list_item, null);
} else {
row = convertView;
}
Video tmpVideo = allVideos.get(position);
String TITLE = tmpVideo.getTITLE();
long vidDur = Long.valueOf(tmpVideo.getDURATION());
String DURATION = String.format(Locale.getDefault(),"%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(vidDur) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(vidDur)),
TimeUnit.MILLISECONDS.toSeconds(vidDur) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(vidDur)));
String filepath = tmpVideo.getDATA();
Bitmap thumbnail = bitmapCache.get(filepath);
TextView tvTitle = (TextView) row.findViewById(R.id.tvListItemVideoTitle);
TextView tvDuration = (TextView) row.findViewById(R.id.tvListItemVideoDuration);
ImageView ivThumbnail = (ImageView) row.findViewById(R.id.ivListItemVideoThumbnail);
tvTitle.setText(TITLE);
tvDuration.setText(DURATION);
if(thumbnail != null){
ivThumbnail.setImageBitmap(thumbnail);
}
return row;
}
}
How should load the thumbnails to decrease the time it takes to load the list adapter? Currently on my device it takes 3-4 seconds before the activity showing the adapter shows, and I only have about 15 videos.
Any suggestions would be greatly appreciated.
Marcus