0

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

Marcus
  • 6,697
  • 11
  • 46
  • 89

2 Answers2

0

Instead of showing videos in listview, Get the thumbnail for your video by passing video path and show them in the ListView.. Use this code to get thumbnail. And also use lazylist to shows images properly.

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path,
MediaStore.Images.Thumbnails.MINI_KIND);
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
0
You can use lazyloading for this,LazyLoading will make your list scroll fast.Follow this library it will help:

https://github.com/nostra13/Android-Universal-Image-Loader

Steps of using lazy loading :

1.)Download jar file from this link:
http://www.java2s.com/Code/Jar/u/Downloaduniversalimageloader161withsrcjar.htm

2.)Create Application File :
import android.app.Application;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        // UNIVERSAL IMAGE LOADER SETUP
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheOnDisc(true).cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .displayer(new FadeInBitmapDisplayer(300)).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .memoryCache(new WeakMemoryCache())
                .discCacheSize(100 * 1024 * 1024).build();

        ImageLoader.getInstance().init(config);
        // END - UNIVERSAL IMAGE LOADER SETUP
    }
}

3.)On your activity in whcih you want to use ,write this it onCreate():

DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.iclauncher) // resource or
                                                                // drawable
                .showImageForEmptyUri(R.drawable.iclauncher) // resource or
                // drawable
                .showImageOnFail(R.drawable.iclauncher) // resource or
                                                            // drawable
                .resetViewBeforeLoading(false) // default
                .delayBeforeLoading(1000).cacheInMemory(true) // default
                .cacheOnDisc(true) // default
                .considerExifParams(true) // default
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
                .bitmapConfig(Bitmap.Config.ARGB_8888) // default
                .displayer(new SimpleBitmapDisplayer()) // default
                .handler(new Handler()) // default
                .build();
        imageLoader = ImageLoader.getInstance();

4.)on your adapter write this: 

    imageLoader.displayImage(alist.get(position).getThumbnails(),
                    holder.ivImage, options, null); // alist.get(position).getThumbnails() is the url of the thumbnail and holder.ivImage is the reference of the imageview where thumbnail is to be placed
Harsh Parikh
  • 3,845
  • 3
  • 14
  • 14