1. 程式人生 > >加載視頻縮略圖列表

加載視頻縮略圖列表

you who exception gettime mage database parent display next()

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方法會比較耗時,應該找找有沒有別的方法可以替代。

加載視頻縮略圖列表