加载视频缩略图列表

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by lbhsot on 2017/6/13.
 */

public class VideoActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private GridLayoutManager manager;
    private VideoActivity.VideoAdapter adapter;

    private Cursor cursor;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.pizidea.imagepicker.R.layout.act_video);
        recyclerView = (RecyclerView) findViewById(com.pizidea.imagepicker.R.id.recyclerview);
        manager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new VideoActivity.SpaceItemDecoration(10));
        initVideoData();
        new Thread(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.setList(getAllVideoFiles(VideoActivity.this));
                    }
                });
            }
        }).start();
    }

    static String[] thumbColumns = new String[]{MediaStore.Video.Thumbnails.DATA, MediaStore.Video.Thumbnails.VIDEO_ID};
    static String[] mediaColumns = new String[]{MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID, MediaStore.Video.Media.TITLE, MediaStore.Video.Media.MIME_TYPE};

    private void initVideoData(){

        //首先检错SDcard上所有的video
        cursor = this.managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
        printLog();
        adapter = new VideoActivity.VideoAdapter(this);
        recyclerView.setAdapter(adapter);
    }

    private static void printLog(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
        Logger.d(sdf.format(date));
    }

    public static ArrayList<VideoActivity.VideoInfo> getAllVideoFiles(Context mContext) {
        VideoActivity.VideoInfo video;
        ArrayList<VideoActivity.VideoInfo> videos = new ArrayList<>();
        ContentResolver contentResolver = mContext.getContentResolver();
        printLog();
        try {
            Cursor cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null,
                    null, null, MediaStore.Video.Media.DATE_MODIFIED + " desc");
            while (cursor.moveToNext()) {
                video = new VideoActivity.VideoInfo();

                if (cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION)) != 0) {
                    video.setDuration(cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION)));
                    video.setVideoPath(cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA)));
                    video.setCreateTime(cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATE_ADDED)));
                    video.setVideoName(cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME)));
                    //获取当前Video对应的ID, 然后根据该ID获取缩略图
                    int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
                    String selection = MediaStore.Video.Thumbnails.VIDEO_ID + "=?";
                    String[] selectionArgs = new String[]{
                            id + ""
                    };
                    ContentResolver crThumb = mContext.getContentResolver();
                    BitmapFactory.Options options=new BitmapFactory.Options();
                    options.inSampleSize = 1;
                    Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id, MediaStore.Video.Thumbnails.MICRO_KIND, options);

                    video.thumbBitmap = curThumb;
                    videos.add(video);
                    printLog();
                }
            }

            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return videos;
    }

    class VideoAdapter extends RecyclerView.Adapter<VideoActivity.VideoAdapter.Holder>{

        private Context context;
        private ArrayList<VideoActivity.VideoInfo> list = new ArrayList<>();

        public VideoAdapter(Context context){
            this.context = context;
        }

        private void setList(ArrayList<VideoActivity.VideoInfo> list){
            this.list = list;
            notifyDataSetChanged();
        }

        @Override
        public VideoActivity.VideoAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new VideoActivity.VideoAdapter.Holder(LayoutInflater.from(context).inflate(com.pizidea.imagepicker.R.layout.item_video, parent, false));
        }

        @Override
        public void onBindViewHolder(VideoActivity.VideoAdapter.Holder holder, int position) {
            if (holder instanceof VideoActivity.VideoAdapter.Holder){
//                Glide.with(context).asBitmap().load(list.get(position).thumbBitmap).into(holder.imageView);
                holder.imageView.setImageBitmap(list.get(position).thumbBitmap);
                Date date = new Date(list.get(position).getDuration());
                SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
                holder.textView.setText(sdf.format(date));
            }
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder{

            TextView textView;
            ImageView imageView;

            public Holder(View itemView) {
                super(itemView);
                imageView = (ImageView) itemView.findViewById(com.pizidea.imagepicker.R.id.image);
                textView = (TextView) itemView.findViewById(com.pizidea.imagepicker.R.id.text);
            }
        }
    }

    static class VideoInfo{
        String filePath;
        String mimeType;
        Bitmap thumbBitmap;
        String title;
        long time;

        long duration;
        String videoPath;
        String createTime;
        String videoName;

        public String getFilePath() {
            return filePath;
        }

        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }

        public String getMimeType() {
            return mimeType;
        }

        public void setMimeType(String mimeType) {
            this.mimeType = mimeType;
        }

        public Bitmap getThumbBitmap() {
            return thumbBitmap;
        }

        public void setThumbBitmap(Bitmap thumbBitmap) {
            this.thumbBitmap = thumbBitmap;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }

        public long getDuration() {
            return duration;
        }

        public void setDuration(long duration) {
            this.duration = duration;
        }

        public String getVideoPath() {
            return videoPath;
        }

        public void setVideoPath(String videoPath) {
            this.videoPath = videoPath;
        }

        public String getCreateTime() {
            return createTime;
        }

        public void setCreateTime(String createTime) {
            this.createTime = createTime;
        }

        public String getVideoName() {
            return videoName;
        }

        public void setVideoName(String videoName) {
            this.videoName = videoName;
        }
    }

    class SpaceItemDecoration extends RecyclerView.ItemDecoration{
        private int space;

        public SpaceItemDecoration(int space){
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left = space;
            outRect.bottom = space;

            if (parent.getChildLayoutPosition(view) % 3 == 0){
                outRect.left = 0;
            }
        }
    }
}

这里会有个问题,getThumbnail方法会比较耗时,应该找找有没有别的方法可以替代。

时间: 2024-10-27 06:26:06

加载视频缩略图列表的相关文章

Android 自定义 ListView 上下拉动&ldquo;刷新最新&rdquo;和&ldquo;加载更多&rdquo;歌曲列表

本文内容 环境 测试数据 项目结构 演示 参考资料 本文演示,上拉刷新最新的歌曲列表,和下拉加载更多的歌曲列表.所谓"刷新最新"和"加载更多"是指日期.演示代码太多,点击此处下载,自己调试一下. 下载 Demo 环境 Windows 2008 R2 64 位 Eclipse ADT V22.6.2,Android 4.4.3 SAMSUNG GT-I9008L,Android OS 2.2.2 测试数据 本演示的歌曲信息,共有 20 条,包括歌手名.歌曲名.时长.缩

短信列表如何让同一个号码的短信只显示一条,刚刚加载短信列表会加载所有的数据列。求指教

============问题描述============ 加载短信列表的话,不做处理.每条数据表的记录都会显示在界面上.现在要把同号码的只显示最新的一条.怎么弄.在线等 ============解决方案1============ 分组来处理 一个号码的所有信息放在一个list里面,显示第一条即可

videojs 动态加载视频

VideoJS dynamic source change via RESTful API 'Undefined' issue with changing RTMP source on compressed video.js build #1612 Dynamically change RTMP source #1399 根据这些问题,处理好了动态加载视频部分,src使用对象数据的方式存放,动态资源在ready的时候放入,嘻嘻

html加载视频文件的方法

在网页中怎么加载视频呢? 在body里加上下面的语句 <video        width=""      height=""       controls>//controls是播放控制 <source src="test.webm">//防止浏览器不兼容,所以2个格式 <source src="test.mp4"> </video> 另外  prelod="aut

安卓异步加载图片(缩略图显示)的实现

/** * 根据指定的图像路径和大小来获取缩略图 * 此方法有两点好处: * 1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度, * 第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图. * 2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使 * 用这个工具生成的图像不会被拉伸. * @param imagePath 图像的路径 * @param width 指定输出图像的

Spring Security 之从数据库加载访问资源列表

关于数据拦截列表,SS官方提供的例子是基于配置的简单实现,例如以下代码: 1 <http> 2 <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/> 3 <intercept-url pattern="/**" access="ROLE_USER" /> 4 <form-login lo

video.js分段自动加载视频【html5视频播放器】

突发奇想的需求,要在官网上放一个一个半小时的视频教程-- 然而,加载成了问题,页面是cshtml的.net混合网站,不知道哪儿的限制,导致视频加不出来. 没有办法,只能前端想办法了. 于是将视频切割成4个 依次加载自动播放.效果还可以. 代码: 引入:<link rel="stylesheet" href="//cdn.bootcss.com/video.js/6.0.0-RC.5/alt/video-js-cdn.min.css"> <scrip

加载视频看完重看,有拖动

package com{ import flash.display.MovieClip; import flash.net.NetConnection; import flash.net.NetStream; import flash.events.AsyncErrorEvent; import flash.events.MouseEvent; import flash.media.Video; import flash.display.SimpleButton; import flash.ev

如何制作带有下拉刷新和下拉加载更多的列表

一般的APP软件都是需要下拉刷新,下拉加载这两个功能的,今天我们就来学习怎么样实现这两个功能. 我们先来讲一下他们的原理,这里我们将采取的方案是使用组合View的方式,先自定义一个布局继承自LinearLayout,然后在这个布局中加入下拉头和ListView这两个子元素,并让这两个子元素纵向排列.初始化的时候,让下拉头向上偏移出屏幕,这样我们看到的就只有ListView了.然后对ListView的touch事件进行监听,如果当前ListView已经滚动到顶部并且手指还在向下拉的话,那就将下拉头